Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and.

Similar presentations


Presentation on theme: "Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and."— Presentation transcript:

1 Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and y must be of the same length. Example: y = [ ] % length is 6 x1= [ ] % length is 6 plot(x1,y) % this is ok Example: y = [ ] % length is 6 x2= [ ] % length is 4 plot(x2,y) will result in an error due to unequal lengths source: HKA & AAB

2 Creating a Plot If only one argument is used, it will become the vertical axis. The horizontal axis will be the corresponding index of the argument. y = [ ] % length is 6 plot(y) % this is ok % Horizontal axis will be the index vector [ ] x = [ ] % length is 4 plot(x) % this is ok % Horizontal axis will be the index vector [ ] MAKE SURE YOU GET THIS source: HKA & AAB

3 { Exercise Plot a sin function:
x = 0 : pi/100 : 2*pi; y = sin(x); plot(x,y) { x goes from 0 to 2π in steps of π/100 Now try: x = 0 : pi/4 : 2*pi; y = sin(x); plot(x,y) why isn’t the curve smooth? source: HKA & AAB

4 Plot Title and Labels To add title and labels to the plot:
xlabel('x = 0:2\pi'); ylabel('Sine of x'); title('Plot of the Sine Function','FontSize',12) Remember, help plot is always useful this is how to write π in a character string or label, ie. not code source: HKA & AAB

5 Specifying Line Styles and Colours
It is possible to specify colour, line styles, and markers (such as plus signs or circles) when you plot your data using the plot command plot (x, y, ‘<color_style_marker>’) <color_style_marker> can be any of: Colour signs: 'c' 'r' 'y' 'g' 'b' 'w' 'k' Line style strings: '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot The marker types are '+', 'o', '*', and 'x', and the filled marker types are 's' for square, 'd' for diamond, etc. plot (x, y, 'r') plot (x, y, 'r*') % note the asterisk * after the r source: HKA & AAB

6 Multiple Data Sets in One Graph
Multiple x-y pair arguments create multiple graphs with a single call to plot. Example: x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5); plot(x,y,x,y2,x,y3); legend command helps identify plots Use single quotes to indicate the label of each dataset legend('sin(x)','sin(x-.25)','sin(x-.5)'); >> help hold on >> help line source: HKA & AAB

7 Figure Windows Matlab automatically opens a figure window if one does not exist; but you can open and name as many as you want – but too many can slow down the system >> figure (n) opens a new figure with n representing the number in the title bar (or changes the figure we handle – see below) >> close(n) >> close all >> clf >> shg % brings the current figure forward source: HKA & AAB

8 Controlling the Axis The axis command provides a number of options for setting the scaling, orientation, and aspect ratio of graphs For axis limits, >> axis([xmin xmax ymin ymax]) >>xlim([xmin xmax]); ylim([ymin ymax]) Can make axis visible/invisible, >> axis on >> axis off The grid command toggles grid lines on and off >> grid on; >> grid off Check: >> help plot3 % try 3D graph of sine & cosine >> axis equal % to get the circle source: HKA & AAB

9 Multiple Plots in One Figure
The subplot command enables displaying multiple plots in the same window or print them on the same piece of paper >> subplot(m,n,i) partitions the figure window into an m- by-n matrix of small subplots and selects the ith subplot for the current plot an example: plot two figures, one that shows the sine and cosine function next to each other and one that shows them one under the other.. source: HKA & AAB

10 Adding text to a plot The text function simply writes text on the plot based on the x-y coordinates. text(x,y, 'your text here') Options for help: >> help text >> help gtext Matlab also supports the TeX typesetting language xlabel(‘\theta’); xlabel('-\pi \leq t \leq \pi'); source: HKA & AAB

11 Handle Graphics Handle Graphics refers to a system of graphics objects that MATLAB uses to implement graphing and visualization functions For example, the following statement creates a figure with a white background colour and without displaying the figure toolbar >> figure('Color','white','Toolbar','none') >> h=figure('Color','white','Toolbar','none') source: HKA & AAB

12 Handle Graphics The following statements create a graph and return a handle to h >> x = 1:10; y = x.^3; h = plot(x,y); You can use the handle h to set the properties of the object (figure). For example, you can set its Colour property: >> set(h,'Color','red') You can also specify properties when you call the plotting function >> h = plot(x,y,'Color','red'); When you query the object properties, >> get(h,'LineWidth') >> get(h) >> h2=gcf % get current figure >> h3=gca % get current axis (also an ‘object’) source: HKA & AAB

13 Handle Graphics >> figure(1) >> x = -pi:pi/10:pi; >> y = tan(sin(x)) - sin(tan(x)); >> plot(x,y,'-rs') >> set(gca,'XTick',-pi:pi/2:pi) >> set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'}) >> xlabel('-\pi \leq \Theta \leq \pi') >> ylabel('func(\Theta)') >> title('Plot of Weird Function') >> text(-pi/4,sin(-pi/4), '\leftarrow Look Here') source: HKA & AAB

14 Figures: Saving and Copying
Can save from File menu of Figure Can copy from File menu of Figure >> help openfig You can save the variables in your workspace using the Save Workspace As item in the figure File menu. You can reload saved data using the Import Data item in the figure File menu You can generate MATLAB code that recreates a figure and the graph it contains by selecting the Generate M-File item from the figure File menu source: HKA & AAB

15 Wrapping up >> help graphics
Use the in-built documentation to place what you have done today into perspective, and to discover more advanced graphics available to you. >> edit graf2d source: HKA & AAB

16 Exercises source: HKA & AAB

17 Exercise Clear the workspace (clear all) and close all open figure windows (close all). Create x = [-2*pi:0.1:2*pi]; Plot x against cos(x) in figure 1 using the default settings (simple line plot, solid blue line) Now replot x against cos(x) in figure 1 but with a solid black line Open figure 10 and plot x against sin(x) as a set of green points only (with no line joining the points ) Close figure 10, keeping figure 1 open, and reselect figure 1 In figure 1, whilst keeping the black line, overplot the figure with red circles where the points are Add to the existing plot sin(x) as a dashed blue line Change the x-axis to show only the range 0 to pi. Add a plot title called 'My favourite plot', an x-axis label called 'x-axis' and a y-axis label called 'y-axis‘ Change the x-axis tic marks and labels to go from 0 to pi in steps of pi/4 source: HKA & AAB

18 Exercise: Multiple Plots in One Figure
Open TBmatlabdata.m and read comments. Run script or type filename on command line Create a pie chart in position 1 of a 2x2 grid showing the proportion of total measles, mumps and chickenpox cases. Add labels to each category and a title. Hint: pie([ sum(A) sum(B) sum(C)], {‘labelA’, ‘labelB’, ‘labelC’}) Create a stacked bar chart in position 2 of a 2x2 grid for measles, mumps and chickenpox. Add labels, legend & title. bar([row vector of 12 steps], [row vector of 3 disease variable names], 'stacked'); Create a stem chart in position 3 of a 2x2 grid of ‘years’ vs ‘cases’ of tuberculosis. Add labels & title. Create the line plot in position 4 of a 2x2 grid of ‘years’ vs ‘infection rate’. Add labels & title.

19 Exercise: Multiple Plots in One Figure


Download ppt "Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and."

Similar presentations


Ads by Google