How do I print a figure such that the printout has the true scale of my axes units?

61 views (last 30 days)
I would like to make a plot where the space between two axis ticks is true to the units. For example, I would like the space between ticks 0 and 1 to be exactly one centimeter. I would then like to print this figure so that I can measure this distance manually on the paper.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Sep 2013
When printing a figure, there are several details that you can use to control the size and scale.
The following is an example on how to set up a plot such that the figure printout has the true scale of the axes units. In this example, the units are centimeters.
% Make a plot
x=7:0.05:15;
y=sin(x);
plot(x,y)
% Force MATLAB to render the figure so that the axes
% are created and the properties are updated
drawnow
% Define the axes' 'units' property
% Note: this does not mean that one cm of the axes equals
% one cm on the axis ticks. The 'position' property
% will also need to be adjusted so that these match
set(gca,'units','centimeters')
% Force the x-axis limits and y-axis limits to honor your settings,
% rather than letting the renderer choose them for you
set(gca,'xlimmode','manual','ylimmode','manual')
% Get the axes position in cm, [locationX locationY sizeX sizeY]
% so that we can reuse the locations
axpos = get(gca,'position');
% Use the existing axes location, and map the axes size (in cm) to the
% axes limits so there is a true size correspondence
set(gca,'position',[axpos(1:2) abs(diff(xlim)) abs(diff(ylim))])
% Optional: Since we are forcing the x-axis limits and y-axis limits,
% the print out may not display the desired tick marks. In order to keep
% these, you can select "File-->Preferences-->Figure Copy Template". Then
% choose "Keep axes limits and tick spacing" in the "Uicontrols and axes"
% Frame. Click on "Apply to Figure" and then "OK".
% Print the figure to paper in real size.
print
% Print to a file in real size and look at the result
print(gcf,'-dpng','-r0','sine.png')
winopen('sine.png')
  1 Comment
Cam Salzberger
Cam Salzberger on 18 Mar 2016
Edited: MathWorks Support Team on 8 Dec 2017
Hey Joseph, I tested this, and get the right dimensions. Maybe the defaults settings for your figures' paper-related properties have changed? Check the default properties for all of the "Printing and Saving" related properties on this page.
For example:
get(groot,'defaultFigurePaperPosition')
You can also reset all of these properties with commands like this:
set(groot,'defaultFigurePaperPosition','default')
Hope this helps!
-Cam

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!