Difference between revisions of "Graphing"
(→Line Plots) |
|||
Line 11: | Line 11: | ||
</source> | </source> | ||
− | The data contains a (100x10) matrix of stock market observations and we are going to plot all of these data point on a single line plot using the | + | The data contains a (100x10) matrix of stock market observations and we are going to plot all of these data point on a single line plot using the <source enclose="none">plot</source> function. |
<source> | <source> | ||
Line 19: | Line 19: | ||
[[File:lineplot1.png]] | [[File:lineplot1.png]] | ||
+ | |||
+ | To modify an existing plot use the <source enclose="none">hold on</source> command. Subsequent plots will then be added to the current figure. For example to add to horizontal lines indicating +/- 1 standard deviation of the stocks data: | ||
<source> | <source> | ||
Line 28: | Line 30: | ||
[[File:lineplot2.png]] | [[File:lineplot2.png]] | ||
+ | |||
+ | Here, in each case the <source enclose="none">plot</source> function is plotting a straight line between two points. The first argument, <source enclose="none">xlim</source> queries the figure to get the coordinates of the start and end of the x-axis (a two element vector), provides the x coordinates. The second argument is a vector containing the corresponding y coordinates of the two points. | ||
+ | |||
+ | The '--r' arguments specifies the plotted line to a red dashed line. Matlab supports a number of line (and marker) types of varying colours; for full details refer to Matlab's documentation on [http://uk.mathworks.com/help/matlab/ref/linespec.html LineSpec]. | ||
= Scatter Diagrams = | = Scatter Diagrams = | ||
+ | |||
+ | <source> | ||
+ | figure | ||
+ | scatter(educ,wage) | ||
+ | </source | ||
+ | [[File:scatterplot1.png]] | ||
+ | |||
+ | |||
+ | <source> | ||
+ | gscatter(educ,wage,female,'rk','do') | ||
+ | xlabel('years of education') | ||
+ | ylabel('average hourly earnings') | ||
+ | legend('female','male','Location','northwest') | ||
+ | </source> | ||
+ | [[File:scatterplot2.png]] | ||
+ | |||
+ | |||
= Histograms = | = Histograms = |
Revision as of 19:49, 23 October 2015
Contents
Introduction
Line Plots
For this example we use one of Matlab's sample dataset to plot a simple line plot showing the time series movement of the stock market data. First Load the data.
load stockreturns
The data contains a (100x10) matrix of stock market observations and we are going to plot all of these data point on a single line plot using the plot
function.
figure
plot(stocks(:))
To modify an existing plot use the hold on
command. Subsequent plots will then be added to the current figure. For example to add to horizontal lines indicating +/- 1 standard deviation of the stocks data:
sd=std(stocks(:);
hold on
plot(xlim, [sd sd],'--r')
plot(xlim, [-sd -sd],'--r')
Here, in each case the plot
function is plotting a straight line between two points. The first argument, xlim
queries the figure to get the coordinates of the start and end of the x-axis (a two element vector), provides the x coordinates. The second argument is a vector containing the corresponding y coordinates of the two points.
The '--r' arguments specifies the plotted line to a red dashed line. Matlab supports a number of line (and marker) types of varying colours; for full details refer to Matlab's documentation on LineSpec.
Scatter Diagrams
figure
scatter(educ,wage)
</source
[[File:scatterplot1.png]]
<source>
gscatter(educ,wage,female,'rk','do')
xlabel('years of education')
ylabel('average hourly earnings')
legend('female','male','Location','northwest')