MATLAB - Tips and Tricks on Creating Better Figures and Plots
25 Jul 2008 Quan Quach 16 comments 5,107 views
In this tutorial, you will learn some useful tricks in generating plots. When I first started using MATLAB, I would always generate plots from the command prompt. After manually naming the x-axis, y-axis, and the title, resizing the plot to the perfect size, and making mods through the Plot Editor, I would paste it into my presentation or report. 95% of the time, I would find myself generating that same plot again because I plotted the wrong data, named the axes incorrectly, zoomed in on the wrong part of the plot, etc.
When we generate plots here at blinkdagger, we always use an m-file to control how we want the plot to look. That way, changes can easily be made. Yes, it’s possible to just manually add in x labels, y labels, titles, legends, and so on. But what happens if you close the figure, or need to make modifications to it later? This is why it’s helpful to do all your modifications through MATLAB commands! In this post, we’ll go over some useful commands when generating plots.
Contents
- Example Plot
- Changing the Size of the Plot
- Changing the Font of your Axes Labels and Titles
- Other Properties
- Conclusion
Example Plot
The following code generates a plot of Blinkdagger’s income as a function of time. It labels the x-axis, y-axis, provides a title, and adds a grid for easy viewing. What can we do to modify this plot to make it more aesthetically pleasing?
t = 0:0.2:10; %sample interval x = 0:length(t)-1; %sample data y = sin(t) - t.^3 + 5*t.^2 + 10*t; %generated data set myPlot1 = figure; %create a new figure named myPlot1 plot(x,y); %plot the data title('Blinkdagger Daily Income') %plot the data xlabel('Day') %label the xaxis ylabel('Income Earned') %label the yaxis grid %add a grid to the plot
Changing the Size of the Plot
Let’s say that I want to adjust the size of the plot. The easiest way is to simply use the mouse to adjust the dimensions of the figure. But what happens if I need to create a series of plots that all need to be the same dimensions? Resizing five different plots to the exact same dimensions can be a difficult task using the mouse! By using the set command, I can change the size and position of the plot; best of all, the process is repeatable. The following command positions the plot at the xy location of (100 pixels,100 pixels) using the lower left corner of your monitor as the reference point. It also sets the plot size to 600 pixels horizontally and 300 pixels vertically.
set(myPlot1,'Position',[100,100,600,300])
By default, my settings are in terms of pixels. If you want to change the measurement type, you can use the following command. The following are possible options: inches, centimeters, normalized, points, pixels, characters.
set(myPlot1,'Units','pixels')
Changing the Font of your Axes Labels and Titles
When I generate plots, I like to make my labels bold so that they stand out a little more. I could use the Plot Editor to accomplish this, but it would be much easier to type in a command.
title('Blinkdagger Daily Income', 'FontWeight','Bold','FontSize',16) %plot the data xlabel('Day','FontWeight','Bold') %label the xaxis ylabel('Income Earned','FontWeight','Bold') %label the yaxis
There are many other properties that you can change in relation to these labels such as FontName, FontAngle, Color, etc.
Other Properties
There are many properties here that you can modify that were not discussed. For instance, you can change the backdrop of the plot a much lighter shade of gray instead of the default gray using the following command:
set(myPlot1,'Color',[0.95 0.95 0.95])
There are a lot of neat properties that you can fiddle with. I recommend that you play around with the settings to learn more about how figures work. Using the set and get command will provide you with much more versatility and flexibility when creating plots!
Conclusion
Creating the perfect plots can be an arduous task, but it can be simplified greatly if you take the time to write a m-file to automate the process. You can go here to learn more about figure properties Similarly, you can go here to learn more about label properties.
Finally, you should visit Loren’s Post on creating Pretty Graphs. It takes what I’ve done here into another stratosphere!
16 Responses to “MATLAB - Tips and Tricks on Creating Better Figures and Plots”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Hey Quan, nice post! I believe a lot of people will benefit from this. Perhaps it would be a good idea to show users how to print the figure to eps. Also, you could take a look at the laprint.m and plotepstex.m from the file exchange. Those are two functions I have found to be particularly useful.
Cheers!
Oskar
Thanks for the great ideas Oskar. I’ve never printed figures to eps though . . . so I need to learn how first! Maybe you can give me a quick tutorial
Quan
Hey Quan,
Nice post. I’m a fan of covering the basics of plotting, which for some reason are often skimmed through in trainings. And I totally agree that plotting from a script is a big time and frustration saver in the long run.
One thing, setting the ‘Position’ and ‘Color’ of the figure can also be done right in the _figure_ command call. I typically do it all up front instead of subsequent _set_ calls. Here’s a possible alternative figure statement for your code above.
myPlot1 = figure(’Position’,[100,100,600,300],’Color’,[0.95 0.95 0.95]);
Best,
Rob
Dear sirs,
I have a Matlab problem, plz help me solve it. Thanks in advance.
I have a figure scanned from a journal. This figure contains a smooth plot. Now I want to get datum from this plot and draw it in my own figure. Can I do this task? Plz help me. Thank you.
thank you
Hi, I have a question, how can I change the grid’s color on a plot. Is there some code? I’ve been looking for it, but I can’t find it. Thanks from Peru.
Hey Franco,
Try the colormap command!
Quan
hi
i would like to make plot with for example 6 lines.
but how can i create the legend which looks like a 2×3 array?
— line1 —line2 —line3
— line4 —line5 —line6
just one row or column doesn’t look nice all the time…
i hope you can help!
Hi Quan Quach,
I have 2 graph that i need to plot.
But i do not know how to set the axis
1. I want to set my y- axis in exponential value and x-axis in number from 0 to 300
2. The second graph is from whole number for both xand y axis.
I try to change the value from your tutorial but unable to get any value.
May I know how do i write the code in the .m file?
IS there any parameter that I need to change at the .fig file
Please help on this
@ dacap
I couldn’t figure this out, but I just got confirmation from a Mathworks guru (Doug Hull) that the legend can only be displayed as a vector. If you’re very interested, you could browse matlab central to see if anyone else has found a solution to that: http://www.mathworks.com/matlabcentral/index.html
@ Joe
I recommend the ’semilogy(x,y)’ function for a logarithmic scale for the vertical axis then using xlim([xmin xmax]) for x limits. Just use plot(x,y) for your 2nd graph. This is done entirely in the .m file
first, thanks for your work it really helps people getting deeper into matlab.
second, i have a very special problem i think.
i have to plot 2 different plots in one figure but with the same x-axis lable. i have to link them in a way that, by using the zoomfunktion on 1 plot, the second has to update to the same x-axis position.
i´ve searched the matlab help, the mathworks forum and your site but jet haven´t found an answer.
thanks in advance from germany
[...] http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-creating-better-figures-and-plots [...]
Hey QQ,
How do you save images /figures in .tiff format on the disk
Hi,
while a ploting a feather plot.
In order to modify the y-axis interval.
I tried with ‘Y lim mode mannual type’
But I didn’t the result.
Please suggest me any another type to modify the axis interval in feather plots.
Hi all,
How to change the axis label color?
Thanks,
Sesa
@Naresh,
You should be able to still use ylim([ymin ymax]) like you would with normal plotting.
@Sesa
In your xlabel line, you can add as many (…’PropertyName’,PropertyValue…) pairs that you’d like.
For example, throw in this after a plot command.
To see the various properties to edit, you can click ‘Show Plot Tools and Dock Figure’ button on the figure toolbar; select the object of interest (xlabel, axes, legend, etc); then click ‘More Properties’ to see all the properties of that object.
Good luck!
-Zane