Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plotting. Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example,

Similar presentations


Presentation on theme: "Plotting. Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example,"— Presentation transcript:

1 Plotting

2 Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example, x values may be in a column vector, and the y values may be in a row vector.

3 LINSPACE function LINSPACE and LOGSPACE functions are often used for generating vectors for plotting operations. Usage: linspace(start-value, end-value, #-of-points) logspace(log(start-value), log(end-value), #-of-points) Example: x= linspace(0, 4*pi, 100); y= sin(x); plot(x, y);

4 Plotting single series Example: x= linspace(0, 4*pi, 100); y= sin(x); plot(x, y);

5 Plotting multiple series Example: x= linspace(0, 4*pi, 100); y= sin(x); plot(x, y); z= 0.5*cos(x); hold on; plot(x, z);

6 Plotting multiple series-2 Example: x= linspace(0, 4*pi, 100); y= sin(x); z= 0.5*cos(x); plot(x, y, x, z); Notice that each serie is represented by a different color.

7 Plotting multiple series-3 It is also possible to have different x-vectors Example: x1= linspace(0, 2*pi, 100); x2= linspace(2*pi, 4*pi, 100); y= sin(x1); z= 0.5*cos(x2); plot(x1, y, x2, z);

8 Plotting multiple series-4 Another alternative is using a single (or multiple) x column vector(s), with multiple y column vectors. In case the x coordinates are the same for all y functions, the same x column vector can be used for all plotted functions. Multiple y column vectors can be grouped into a matrix. In case the x coordinates are different for y functions, different x column vectors can also be grouped into a matrix.

9 Plotting multiple series-4 (example) x= linspace(0, 4*pi, 100)'; y1= sin(x); y2= 4*cos(x); y3= x.*sin(x); y= [y1 y2 y3]; plot(x, y);

10 Adding titles Done by using title(), xlabel() and ylabel(). Example: x= linspace(0, 4*pi, 100); y= sin(x); plot(x, y); title('Plot of sin(x)'); xlabel('x (radians)'); ylabel('sin(x)');

11 Adding legend x= linspace(0, 4*pi, 100); y1= sin(x); y2= 4*cos(x); y3= x.*sin(x); plot(x, y1, x, y2, x, y3); legend('sin(x)', '4cos(x)', 'x.sin(x)'); title('Various functions'); xlabel('x (radians)');

12 Legend placement General form of the legend command: legend(legend1, legend2,..., legendn, place) Place constant has the following meanings:  1: Top right  2: Top left  3: Bottom left  4: Bottom right If the Place constant is omitted, it is assumed to be 1.

13 Formatting series In MATLAB plots, it is also possible to modify the visual appearance of the plotted lines. The following properties can be modified:  Line styles,  Line colors,  Markers at data points. The relevant format codes are written after each x-y vector pair that will receive the formatting. Example: plot(xvector1, yvector1, format1,..., xvectorn, yvectorn, formatn)

14 Marker, line style & color codes.Point oCircle xX-mark +Plus *Star sSquare dDiamond vTriangle (down) ^Triangle (up) <Triangle (left) >Triangle (right) pPentagram hHexagram -Solid :Dotted -.Dashdot --Dashed yYellow mMagenta cCyan rRed gGreen bBlue wWhite kBlack

15 Formatting series: Example The following code formats each serie differently: x= linspace(0, 4*pi, 100); y1= sin(x); y2= 4*cos(x); y3= x.*sin(x); plot(x, y1, 'gs', x, y2, 'kp:', x, y3, 'r-');

16 Background color The background color can be modified using the WHITEBG command. Using the command without arguments toggles the background color between black and white. It is also possible to specify a different backround color using the color codes defined for lines. Example: >>whitebg('y');% Yellow background

17 Grid GRID ON: Places a grid on the current plot. GRID OFF: Removes the grid from the current plot. GRID: Toggles grid.

18 Axes limits The lower and upper limits of the plot in the x- and the y-directions can be specified using the AXIS command. Syntax:  AXIS([Xmin Xmax Ymin Ymax])  AXIS AUTO

19 Grid & axis example grid on; axis([0 2*pi -1 5]);

20 Placing text on a plot Done using the text command: text(x-coord, y-coord, text-string); Examples: text(2, 4, 'Sines'); text(2, 3, '\alpha\beta'); text(2, 2, '\Gamma\Omega');


Download ppt "Plotting. Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example,"

Similar presentations


Ads by Google