Connexions

You are here: Home » Content » An Introduction to MATLAB » Graphical representation of data in MATLAB

Recently Viewed

This feature requires Javascript to be enabled.

Inside Collection (Course):

Course by: Anders Gjendemsjø. E-mail the author

Graphical representation of data in MATLAB

Module by: Anders Gjendemsjø. E-mail the author

Summary: Introduces graphical representation of data in MATLAB.

Graphical representation of data in MATLAB

MATLAB provides a great variety of functions and techniques for graphical display of data. The flexibility and ease of use of MATLAB's plotting tools is one of its key strengths. In MATLAB graphs are shown in a figure window. Several figure windows can be displayed simultaneously, but only one is active. All graphing commands are applied to the active figure. The command figure(n)will activate figure number n or create a new figure indexed by n.

Tools for plotting

In this section we present some of the most commonly used functions for plotting in MATLAB.

• plot- The plot and stem functions can take a large number of arguments, see help plot and help stem. For example the line type and color can easily be changed. plot(y) plots the values in vector yversus their index. plot(x,y) plots the values in vector yversus x. The plot function produces a piecewise linear graph between its data values. With enough data points it looks continuous.
• stem- Using stem(y)the data sequence yis plotted as stems from the x-axis terminated with circles for the data values. stem is the natural way of plotting sequences. stem(x,y) plots the data sequence y at the values specified in x.
• xlabel('string')- Labels the x-axis with string.
• ylabel('string')- Labels the y-axis with string.
• title('string')- Gives the plot the title string.

To illustrate this consider the following example.

Example 1

In this example we plot the function y = x2 for x 2 [-2; 2].

x = -2:0.2:2;

 

 y = x.^2; 

 

 figure(1); 

 

 plot(x,y); 

 

 xlabel('x'); 

 

 ylabel('y=x^2'); 

 

 title('Simple plot'); 

 

 figure(2); 

 

 stem(x,y); 

 

 xlabel('x'); 

 

 ylabel('y=x^2'); 

 

 title('Simple stem plot'); 

This code produces the following two figures.

Some more commands that can be helpful when working with plots:

• hold on / off - Normally hold is off. This means that the plot command replaces the current plot with the new one. To add a new plot to an existing graph use hold on. If you want to overwrite the current plot again, use hold off. 
•  legend('plot1','plot2',...,'plot N')- The legend command provides an easy way to identify individual plots when there are more than one per figure. A legend box will be added with strings matched to the plots. 
•  axis([xmin xmax ymin ymax])- Use the axis command to set the axis as you wish. Use axis on/off to toggle the axis on and off respectively. 
•  subplot(m,n,p) -Divides the figure window into m rows, n columns and selects the pp'th subplot as the current plot, e.g subplot(2,1,1) divides the figure in two and selects the upper part. subplot(2,1,2) selects the lower part. 
•  grid on/off - This command adds or removes a rectangular grid to your plot.

Example 2

This example illustrates hold, legend and axis.

 x = -3:0.1:3; y1 = -x.^2; y2 = x.^2; 

 

 figure(1); 

 

 plot(x,y1); 

 

 hold on; 

 

 plot(x,y2,'--'); 

 

 hold off; 

 

 xlabel('x'); 

 

 ylabel('y_1=-x^2 and y_2=x^2'); 

 

 legend('y_1=-x^2','y_2=x^2'); 

 

 figure(2); 

 

 plot(x,y1); 

 

 hold on; 

 

 plot(x,y2,'--'); 

 

 hold off; 

 

 xlabel('x'); 

 

 ylabel('y_1=-x^2 and y_2=x^2'); 

 

 legend('y_1=-x^2','y_2=x^2'); 

 

 axis([-1 1 -10 10]); 

The result is shown below.

Example 3

In this example we illustrate subplot and grid.

 x = -3:0.2:3; y1 = -x.^2; y2 = x.^2; 

 

 

 subplot(2,1,1); 

 

 plot(x,y1); 

 

 xlabel('x'); ylabel('y_1=-x^2'); 

 

 grid on; 

 

 subplot(2,1,2); 

 

 plot(x,y2); 

 

 xlabel('x'); 

 

 ylabel('y_2=x^2'); 

Now, the result is shown below.

 Printing and exporting graphicsAfter you have created your figures you may want to print them or export them to graphic files. In the "File" menu use "Print" to print the figure or "Save As" to save your figure to one of the many available graphics formats. Using these options should be sufficient in most cases, but there are also a large number of adjustments available by using "Export setup", "Page Setup" and "Print Setup".To streamline the graphics exportation, take a look at exportfig package at Mathworks.com, URL: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=727. 3D Graphics We end this module on graphics with a sneak peek into 3D plots. The new functions here are meshgrid and mesh. In the example below we see that meshgridproduces xand yvectors suitable for 3D plotting and that mesh(x,y,z) plots z as a function of both x and y.Example 4Example: Creating our first 3D plot.[x,y] = meshgrid(-3:.1:3);z = x.^2+y.^2;mesh(x,y,z);xlabel('x');ylabel('y');zlabel('z=x^2+y^2');This code gives us the following 3D plot. 
 Collection Navigation « Previous module in collection Using MATLAB Collection home: An Introduction to MATLAB Next module in collection » Scripts and Functions in MATLAB Content actions Share content Share module: Give feedback: E-mail the collection author | E-mail the module author Download: Collection as: PDF | EPUB (?) What is an EPUB file? EPUB is an electronic book format that can be read on a variety of mobile devices. Downloading to a reading device For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link. | More downloads ... Module as: PDF | EPUB (?) What is an EPUB file? EPUB is an electronic book format that can be read on a variety of mobile devices. Downloading to a reading device For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link. | More downloads ... Add: Collection to: My Favorites Login Required (?) 'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'. | A lens I own Login Required (?) Definition of a lens Lenses A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust. What is in a lens? Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content. Who can create a lens? Any individual member, a community, or a respected organization. What are tags? Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens. | External bookmarks Module to: My Favorites Login Required (?) 'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'. | A lens I own Login Required (?) Definition of a lens Lenses A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust. What is in a lens? Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content. Who can create a lens? Any individual member, a community, or a respected organization. What are tags? Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens. | External bookmarks Footer More about this module: Metadata | Downloads | Version History How to reuse and attribute this content How to cite and attribute this content This work is licensed by Anders Gjendemsjø under a Creative Commons Attribution License (CC-BY 2.0), and is an Open Educational Resource. Last edited by Anders Gjendemsjø on Jan 20, 2006 6:21 am -0600. 
 
 var _gaq = _gaq || []; function trackthisGoogleAnalytics(strCode) { try { _gaq.push(['user._setAccount', strCode]); _gaq.push(['user._trackPageview']); } catch(err) {} } var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-7903479-1']); _gaq.push(['_setDomainName', '.cnx.org']); _gaq.push(['_trackPageview']); _gaq.push(['hewlett._setAccount', 'UA-5033010-1']); _gaq.push(['hewlett._setDomainName', '.cnx.org']); _gaq.push(['hewlett._trackPageview']);