Matlab can generate some notorisoly ugly plots... but for many grad student's it's our bread and butter. So here are a couple tricks I use to generate better looking matlab plots:
1) Use Matlab's built-in LaTeX interpreter for your figure titles and labels
It produces really nice looking titles and axes labels, even for non-math text.
title('$rho=frac{left< c_1^prime c_2^prime right>}{sigma_1 sigma_2}$ for Img 1000','Interpreter','latex');
You can also set latex to be your interperter for an entire figure from the start, which helps clean up the code a bit in the long run:
set(0,’defaulttextinterpreter’,'latex’)
(Thanks CowMan!)
For more info see Matlab's help file on Text Properties.
2) Predefine Figure Properties so your look will be consistant
Before I start plotting, I usually define a figure with some pre-made settings. I think about where I'm going to use the figure (is it for a paper, or a presentation?) and what size and aspect ratio i'll want.
Figure1=figure(1); clf(Figure1); set(Figure1,'PaperUnits','inches',... 'PaperSize',[6.5 4.5],... 'PaperPosition',[0,0,6.5,4.5]);
This makes a figure that's going to be 6.5x4.5 inches (a good size for full width, half page figures in a paper). It sets the margins to 0, so the figure will fill up the whole area. If you wanted a half-inch margin around the figure, you would change the last line to
'PaperPosition',[.5,.5,5.5,3.5]);
This starts the image at .5, .5 and made the image height and width a full inch (0.5 x 2) smaller.
Note: this is for exporting, wont translate to how the figure looks on the screen. For that, use:
set(Figure1,'Units','inches','Position',[1 1,6.5,4.5])
. This will make the Figure the correct dimensions, but also requires you to set the location on screen (Here, 1, 1 which is towards the left center of my screen). This can be useful for defining how multiple plots show up on screen.
Also note this clears the figure if it already exists.
3) Use subplot or subaxis to effectively display multiple pieces of information.
You can even have a subplot span multiple positions by providing multiple instead of single positions, like this:
subplot(3,2,[1 3])
Update: I recently discovered a piece of code in the file exchange called subaxis, which you can use in place of the subplot command. It allows you to control the spacing between subplots and the margins around your plots, which makes for a much better use of space!
subaxis(1,3,3,'Spacing', 0.03, 'Padding', 0, 'Margin', 0)
4) Annotate your plots!
Use stuff like Rectangles, Arrows, and Text Boxes to convey or call out information. For example the title text on the figure above was made using the command:
annotation(Figure3,'textbox',... [0.4 0.8 0.3 0.2],... 'String',{'8/24/11 - Re 1490'},... 'FitBoxToText','off',... 'LineWidth',0,'EdgeColor','none',... 'Interpreter','latex','FontSize',18,... 'HorizontalAlignment','center');
Similarly, the rectangle was super easy to draw:
imshow(Img,'XData',Xs,'YData',Ys);axis on;hold on; rectangle('Position',[BoxX,BoxY,Xw,Yw]);hold off;
5) Take some freedom's with colormaps!
you can reverse matlab's standard colormaps like this:
colormap(flipud(gray));
You can also define your own colormaps like this:
B(1,:)= [0:1/31:1 ones(1,32)]; B(2,:)= [0:1/31:1 1:-1/31:0]; B(3,:)= [ones(1,32) 1:-1/31:0]; colormap(B')
Here, we're defining a 64x3 matrix, defining a color scale where the first column defines Red's behavior, the second defines Green's, and the third defines Blues's. Looking at the code, Red goes from 0 to 1 on the left side of 32 (the mid point) and then is uniformly 1 on the right side (from 32 to 64). Green increases to 1 and then decreases back to zero. Blue does just the opposite of Red, Starting at one on the right and then reducing to zero at the left. The result means the Left most side is 0,0,1 (Blue), the middle is 1,1,1 (White) and the right hand side is 1,0,0 (Red).
I've also outlined how to create a logarithmic colorscale in another post. That can be super helpful if you have data that scales non-linearly.
6) Use multiple colormaps in the same subfigure
Many times I want to display one piece of information that is best in one colormap (e.g, the Red White Blue one above) next to another piece of information that is best in a different colormap (e.g., the reverse grayscale). There's a handy piece of code called freezeColors in the mathworks file exchange that can handle this problem.
7) Customize your Axes!
Use commands found on the Axes Properties page to make your axes look just how you want them.
set(gca,'TickLength',[0 0],'box','on','XTick',[],'YTick',[],'YDir', 'reverse');
In addition, there are a few axes specific commands you can call to make your images look good. I like using the following ones a lot:
axis image;axis([xmin xmax ymin ymax]);
8) Manually export your plots to PDFs with constant settings using print
The export figure GUI in matlab is kind of annoying. Having one line of code that automatically saves your figure to your desired format is much better! This works best if you've done step 2 to define how the figure will fit on the page.
When I'm done plotting I use this command:
print('-dpdf','-r500',['Theory Compare.pdf'])
To print to a PDF from matlab. I also will sometimes print to a TIFF:
print -r500 -dtiff Re850meanstatsB.tif
The -r500 pre-defines the resolution. This helps alleviate the bad resolution of many standard Matlab plots.
8) Make an animation of your plots to show temporal changes!
I've outlined how to do this in a separate post here!
I hope to extend this post with some more tips as I think of them. Are there any tips I missed? Anything you'd like to know to how to do?