# 0.2 Graphical representation of data in matlab

 Page 1 / 1
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 andease of use of MATLAB's plotting tools is one of its key strengths. In MATLAB graphs are shown in a figure window. Several figurewindows 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 theline type and color can easily be changed. `plot(y)` plots the values in vector `y` versus their index. `plot(x,y)` plots the values in vector `y` versus `x` . The `plot` function produces a piecewise linear graph between its data values. With enough data points it lookscontinuous.
• `stem` - Using `stem(y)` the data sequence `y` is 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.

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 anew 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 legendbox 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 `p` p'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.

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.

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 graphics

After 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 theseoptions 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: (External Link) .

## 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 `meshgrid` produces `x` and `y` vectors suitable for 3D plotting and that `mesh(x,y,z)` plots `z` as a function of both `x` and `y` .

Example: 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.

the art of managing the production, distribution and consumption.
what is economics
okk
damfash
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
It's the allocation of scarce resources.
Fred
Dishan
marginal utility is the additional satisfaction one derives from consuming additional unit of a good or service.
Fred
I know the definition, but I don't understand its meaning.
Dishan
what is the must definition of economic please?
Nurudeen
demand lfs
Alpha
Economics is derived from the word Oikonomia which means management of household things. Thus, Economics is a study of household things with the constrains of allocating scare resources.
Dishan
what is Open Market Operation
dominating middlemen men activities circumstances
what Equilibrium price
what is gap
mirwais
who is good with the indifference curve
Dexter
What is diseconomic
what are the types of goods
WARIDI
how can price determination be the central problem of micro economics
marginal cost formula
you should differentiate the total cost function in order to get marginal cost function then you can get marginal cost from it
boniphace
Foday
ok
Foday
how can price determination be the central problem if micro economics
simon
formula of cross elasticity of demand
what is ceteris paribus
what is ceteris parabus
Priyanka
Ceteris paribus - Literally, "other things being equal"; usually used in economics to indicate that all variables except the ones specified are assumed not to change.
Abdullah
What is broker
scor
land is natural resources that is made by nature
scor
What is broker
scor
what is land
kafui
What is broker
scor
land is natural resources that is made by nature
scor
whats poppina nigga turn it up for a minute get it
what is this?
Philo
am from nigeria@ pilo
Frank
am from nigeria@ pilo
Frank
so
owusu
what is production possibility frontier
owusu
it's a summary of opportunity cost depicted on a curve.
okhiria
please help me solve this question with the aid of appropriate diagrams explain how each of the following changes will affect the market price and quantity of bread 1. A
ok let me know some of the questions please.
Effah
ok am not wit some if den nw buh by tommorow I shall get Dem
Hi guys can I get Adam Smith's WEALTH OF NATIONS fo sale?
Ukpen
hello I'm Babaisa alhaji Mustapha. I'm studying Economics in the university of Maiduguri
Babaisa
okay
Humaira
my name is faisal Yahaya. i studied economics at Kaduna state university before proceeding to West African union university benin republic for masters
Faisal
Mannan
Wat d meaning of management
disaster management cycle
cooperate social responsibility
igwe
Fedric Wilson Taylor also define management as the act of knowing what to do and seeing that it is done in the best and cheapest way
OLANIYI
Difference between extinct and extici spicies
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Got questions? Join the online conversation and get instant answers!