Matlab Logo 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

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

picture01

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])

picture02

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

picture03

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])

picture04

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!