08 December, 2011

PLOTTING 2-D GRAPHS USING MATLAB


2-D graphs in MATLAB (7.10.0 R-2010a) can be easily plotted using ‘plot ( )’ function. I am not describing the details of  MATLAB and using plot function.
An example of plotting a 2-D graph (sine curve):
x=0:0.01:2*pi;
y=sin(x);    
plot(x,y);

An example of plotting two graphs simultaneously:
x=0:0.01:2*pi;
y=sin(x);
z=cos(x);
plot(x,y);
hold on
plot(x,z,'--r'); %’--r’plots dashed red colored graph
hold off

Title, x-axis label, y-axis label, legend, arrow can be added using the option ‘Insert’ in figure window or using the Plot Edit Toolbar.
Grid lines can also be added using property editor-axes (go to edit option and then click axes properties) or by simply checking the edit plot option in tools and then right clicking on graph and checking the grid option. In the figure grid lines are added for only y-axis.
Legends can be edited by double clicking on them (initially you will see data1 and data2 written). Also properties of it can be edited using the property editor.
I have changed the x-limits to 8(explained later) so that the legend does not hide a part of graph. This is not necessary, but doing this makes the graph look neat and realizable at every point.
Edit Plot option in ‘Tools’ can be useful to avoid certain commands or alterations in commands and also properties of  the figure, graph paper, graph can be changed.

1)  FIGURE PROPERTIES
Check the Edit Plot option in tools and then left-click outside the graph i.e. the area which is purple in the figure. This gets the rectangle formed by 8 square bullets at the outermost of the figure. Now right click in the same area and you will get four options: Color, Close Figure, Show Property Editor (by which you can give the figure window a desired name and get the color options), Show M-Code.

2) GRAPH PAPER PROPERTIES
Edit Plot option being checked; click inside the graph paper and not on the graph drawn such that the eight rectangular bullets appear on the boundary of graph paper as shown in figure and then right click. 
Add Data option is a useful one and is discussed later.
Color option allows you to change the color of graph paper (here I have changed it to light green).
Show Property Editor option opens property editor-axes as shown in figure. From here you can give labels, set limits of the axes (here set to 8), grids for respective axis can be added and many more properties can be changed.
3) GRAPH PROPERTIES
In order to study graph properties we take a different example:
x=0:0.1:2*pi;
y=sin(x);
plot(x,y);


 

To edit the graph or plot we follow the same procedure i.e. check the edit plot option and now click on the graph or plot and nowhere else and the rectangular bullets will appear on the graph and now right click it.
Option color allows to change the color of plot, Line Style (way of joining points or plotting) can be set to solid, dash, dot, dash-dot, none.
In the figure shown below color of graph has been changed to black and Line Style is kept solid.


Line width is thickness of the graph and 'Marker' is another beautiful option which can make the graph look better, and its size can be adjusted by 'Marker Size'.
In the figure drawn below marker selected is ‘*’ as it can be seen checked and various types of it can be selected.
These options can be added by commands also and it is important to know them. We can edit the graph from the figure window but if multiple graphs are plotted like in case of 3-D figure where 200 or more graphs can be plotted on the same graph paper editing is not possible as we will have to select them one by one and do the required changes. Commands are convenient in this case as they can be put in the loop and the required condition will be applied to all the graphs. This you can evident from my next post.

Show Property Editor opens Property Editor-Lineseries window and similar options of Line style, Line width, Marker, Marker Size are available and we can opt for different Line Style color and different marker color which we earlier couldn’t do.
X data source and Y data source can be also specified by just putting the expression in the respective column. In the figure shown below Ydata source has been put ‘cos(x).*exp(-0.2*x)’ and x dat source auto and the resultant graph is shown.
In the Edit Plot mode, in all the possible three domains: figure, graph paper and graph explained earlier, Property Editor function is the best function as you can access all possible options for editing and many additional ones too, which are useful like different color can be filled in markers (closed), outline can be of different color and the graph can be given a third color. In the edited damped sine the marker is filled with red, outlined with blue and graph is dashed black. A simple graph of damped sine can be drawn simply by defining x vector and putting y= sin(x).*exp(-0.2*x) and can be made presentable by editing.
graph without editing
same graph edited using various property editors
2-D GRAPH TYPES
Many types of graphs can be plotted using MATLAB : bar graphs, pie charts etc.
I will explain the useful graph types in brief, so that they can be used for basic work. Let us assume the following data of a company:

  Year
2005
2006
2007
2008
2009
2010
Company A profit(in crores)

  500.43

 562.62

 357.81

 597.39

 624.61

  731.98

Now if we simply plot the data, the graph is not very much understandable, as shown

The x-axis is plotted with step size 0.5 i.e. 2005, 2005.5, 2006… In order to change it to one go to Property Editor-Axes as shown in above figure. Click the ‘Ticks..’ option, ‘edit axes ticks’ window will pop up as shown and then check the ‘Step by’ option and put 1 in the text box and click apply.
Graph can be made presentable by using bar graphs.

BAR GRAPHS
For the above plotted graph or the normal plot or Lineseries graph, go to Property Editor-Axes and change the Plot Type option to bar as shown in the figure 2.
Also the Plot Type can be changed using 'Add Data' option which I earlier mentioned in graph paper properties and this option can be selected by Plot Browser also.
Direct command bar(var1,var2) can be used to plot bar graphs. Commands can also be used to plot a certain color bar graph, line width can also be specified by commands. 

figure1 bar graph
But still it is not presentable. We can again follow the plot editing techniques using ‘Edit Plot’ option in the three domains. But this time, Property Editor in the third domain will be Property Editor-Barseries as shown in figure 2 and after editing the graph will appear as in figure shown below. Bar width and color can be changed by respective options, in Property Editor-Barseries.
figure-2 bar graph
Data of the corresponding bar can be written above the respective bar with the help of Text Box and properties of it can also be edited by double-clicking on it. Property Editor-Text box will open up and properties can be modified.  

I plotted another dashed lineseries red graph to make it look better. However, it is not necessary. The final edited graph, may look like shown below: 

AREA GRAPH
It fills the area under lineseries graph with the color of graph and then the outline or plot color can be changed. Change the Plot Type to Area after plotting the graph. area(var1,var2) command can also be used to plot the graph.
                                      
STAIR AND STEM GRAPH
Commands stairs(x), stairs(x,y) etc. can be used for stairs and stem(x), stem(x,y) can be used for plotting stem graph. These can also be selected from the Plot Type or the Plot catalog and editing can be done in edit plot mode.


PIE CHARTS
Pie charts are used for one variable. Consider in a survey if 52% of students opt engineering, 14% medical,18% commerce  and remaining 16% other fields and we are required to draw a pie chart then following can be done:

a=[52 14 18 16 ];    %declare the vector with data to be plotted
pie(a)                     % forms a pie chart for data a

Each patch can be edited e.g color, outline etc. and a patch can be separated out by defining another vector whose dimensions are equal to that of vector of data. If we are required to separate other fields students patch. Then 4th value should be put1and rest zero,
c=[0 0 0 1];
pie(s,c)
Pie charts can also be made from the Plot Catalog from Figure palette or from Plot Type.

3-D graphs, 3-D pie charts, 3-D bar graphs, 3-D figures can also be drawn using MATLAB which I will explain in my next post.


5 comments: