print figure with fixed border

4 views (last 30 days)
Kai
Kai on 5 Feb 2020
How can I create and print figures with a fixed, predefined border?
The context is to create a series of pictures to be used in a beamer presentation in LaTeX. For instance, one might want to show a circle on a first slide and then add some normal vectors to the image on a second slide. For a nice presentation it is necessary that the circle maintains size and position on each slide.
Consider the following code:
% plot and print unit circle
t = linspace(0,2*pi);
plot(sin(t),cos(t))
axis equal
axis off
print('-dpng','circle')
% add normal vectors and print again
hold on
quiver(sin(t),cos(t),1.2*sin(t),1.2*cos(t))
print('-dpng','circle_normals')
This results in circle.jpg and circle_normal.jpg:
circle.pngcircle_normals.png
As you can see, the circle in the second picture is smaller (or rather seems smaller). This makes sense cause the print command of MATLAB creates a picture with a tight border around the plotted data, and normal vector arrows are defining that border in the second picture. Adding these two .png-files to the LaTeX beamer presentation will result in the circle being placed and sized differently, even if the LaTeX positioning is the same.
What I need is a method to adapt the border of the first picture to the border of the second picture. I have spent some (actually a lot) of time to come up with:
My approaches:
1. Define the borders of what MATLAB should print: I haven't found any suitable command/ object property. For instance, xlim (ylim, zlim) only changes which parts of lines etc. are visualized, not which area is cropped/ printed. So for instance, setting xlim(-100,100) won't have any effect for the .png-file here, whereas setting xlim(-0.5,0.5) results in only part of the circle being shown in the .png-file. I haven't quite understood what PaperPosition, InnerPosition and OuterPosition do, but it seemed to me that they are not related to this. I searched around the plotbrowser, but didnt find anything useful. The perfect solution would be some "border property", so I could just say "fig1.border = fig2.border", i.e. store the border as a variable and use it for all pictures in the series. Even better would be to have a border property for each dimension.
2. Use the export_fig toolbox: If you define the camera (campos, camtarget) manually and use the -nocrop option for export_fig, this works in the sense that you get images where the circle has the same positioning and size. However, there is a lot of white space. export_fig basically gives you something like a screenshot of the figure window, so reducing the white space comes down to finding a better camera setup. But even then it is highly unsatisfactory cause the "MATLAB camera" always has the same width-height proportion as I feel. Thus, in this circle case, however tight you place the camera at the top and bottom, there will always be white space to the left and right. This might be due to my lack of knowledge about the camera settings, but still, setting the camera options manually is not a nice solution cause you need to find a good positioning again for each new series of pictures.
3. Plot a border: One solution that I could come up with was to simply "plot the border". In here, for instance:
% plot rectangle around the circle
plot([-1.3,1.3,1.3,-1.3,-1.3],[-1.3,-1.3,1.3,1.3,-1.3],'black')
Plotting this (sufficiently large) rectangle redefines the border where the print command starts cropping. However, I do not wish to always use some rectangle around my pictures. I thought the solution to this would be easy: simply setting the 'Color' to 'white', but then I realised that the print command simply ignored my white line and started cropping again. Using some "nearly white" color (like [0.999,0.999,0.999]) is actually the solution that I would like to work with for now. However, as with the export_fig approach, this solution requires me to figure out where to place the border everytime I create a new series of pictures. Especially in 3D-plots this is quite annoying, and this approach just seems so unnatural to me.
As you can see, all these solutions are rather unsatisfactory, or at least not optimal. I hope to find a simpler and better way in here.

Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!