plottingmypages.valdosta.edu/rpmihail/teaching/s17/cs1340/slides/plot.pdfplots visualize one of the...

24
Plotting Dr. Mihail February 17, 2015 (Dr. Mihail) Plots February 17, 2015 1 / 24

Upload: others

Post on 28-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plotting

Dr. Mihail

February 17, 2015

(Dr. Mihail) Plots February 17, 2015 1 / 24

Page 2: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plots

Visualize

One of the most important uses of packages like MATLAB is their richdata visualization capability. Visualizing data often yields insights thatwould otherwise be impossible.

(Dr. Mihail) Plots February 17, 2015 2 / 24

Page 3: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plots

Figures

Graphic visulizations in MATLAB are produced in Figure windows. TheMATLAB syntax to create a figure is the following:

figure(figure_number)

example:

figure(1)

will create a new window titled “Figure 1”. There can be several Figurewindows opened simultaneously. Switching between them is done usingthe figure() function.

(Dr. Mihail) Plots February 17, 2015 3 / 24

Page 4: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Figure Window

(Dr. Mihail) Plots February 17, 2015 4 / 24

Page 5: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Clearing Figure Window

To clear, use the clf command. Example:

>> figure(1)

>> clf

>>

(Dr. Mihail) Plots February 17, 2015 5 / 24

Page 6: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Simple 2D Plots

Say we want to plot the function:

y = x2

Entire script

x = linspace(-6, 6, 100); % 100 equally spaced x’s

y = x.^2;

figure(1);clf;

plot(x, y);

(Dr. Mihail) Plots February 17, 2015 6 / 24

Page 7: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

1D Function Plot

The result:

(Dr. Mihail) Plots February 17, 2015 7 / 24

Page 8: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

1D Function Plot

Notes:

plot(x, y) default behavior is to connect points in order (x1, y1)connected to (x2, y2), connected to (x3, y3), etc.

x and y can be either column or row vectors

(Dr. Mihail) Plots February 17, 2015 8 / 24

Page 9: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Simple 2D Plots

Let’s change the number of samples to 6:

x = linspace(-6, 6, 6); % 6 equally spaced x’s

y = x.^2;

figure(1);clf;

plot(x, y);

What will the graph look like?

(Dr. Mihail) Plots February 17, 2015 9 / 24

Page 10: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

1D Function Plot

The choppy result:

(Dr. Mihail) Plots February 17, 2015 10 / 24

Page 11: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Simple 2D Plots

Multiple graphs same figure

To graph multiple plots on the same figure, use hold on;. This will holdon to existing graphics in the Figure window until hold off; is called.For example:

x = linspace(-3, 3, 100);

y1 = x.^2;

y2 = sin(x);

figure(1);clf;hold on;

plot(x, y1);

plot(x, y2);

(Dr. Mihail) Plots February 17, 2015 11 / 24

Page 12: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

Note how MATLAB automatically sets the x-limits and y -limits.

(Dr. Mihail) Plots February 17, 2015 12 / 24

Page 13: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

Each of the two functions has 100 data points. We can change the defaultbehavior of plot using the following syntax plot(x, y, LineSpec),where LineSpec is a string that sets the line style, marker symbol andcolor. For more info, type >> doc plot.

(Dr. Mihail) Plots February 17, 2015 13 / 24

Page 14: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

LineSpec

plot(x, y, LineSpec)

Line style, marker symbol, and color, specified as a string. The elements ofthe string can appear in any order, and you can omit one or more optionsfrom the string specifier. If you omit the line style and specify the markercharacter, then the plot shows only the marker and no line.If Y is a matrix and you specify a color with LineSpec, then all lines areplotted using the specified color. If you specify a marker type or line styleand do not specify a color, then MATLAB cycles through the predefinedcolor order.Example: Example: ’--or’ is a red dashed line with circle markers

(Dr. Mihail) Plots February 17, 2015 14 / 24

Page 15: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Simple 2D Plots

Multiple graphs same figure

Let’s change our previous example, make quadratic dotted green and thesine wave a red dashed line.

x = linspace(-3, 3, 100);

y1 = x.^2;

y2 = sin(x);

figure(1);clf;hold on;

plot(x, y1, ’.g’);

plot(x, y2, ’--r’);

(Dr. Mihail) Plots February 17, 2015 15 / 24

Page 16: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

(Dr. Mihail) Plots February 17, 2015 16 / 24

Page 17: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plot Properties

Useful plots will have their axes labeled and a title. In MATLAB:

Title: title(’example title’)

X-Axis Label: xlabel(’example x-label title’)

Y-Axis Label: ylabel(’example y-label title’)

Example:

x = linspace(-3, 3, 100);

y1 = x.^2;y2 = sin(x);

figure(1);clf;hold on;

plot(x, y1, ’.g’);

plot(x, y2, ’--r’);

xlabel(’x’); ylabel(’y’);title(’Quadratic and Sine’);

(Dr. Mihail) Plots February 17, 2015 17 / 24

Page 18: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

(Dr. Mihail) Plots February 17, 2015 18 / 24

Page 19: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plot Properties

What about a legend? In MATLAB:legend({’name of plot1’, ’name of plot 2’, ...});. Example:

x = linspace(-3, 3, 100);

y1 = x.^2;y2 = sin(x);

figure(1);clf;hold on;

plot(x, y1, ’.g’);

plot(x, y2, ’--r’);

xlabel(’x’); ylabel(’y’);title(’Quadratic and Sine’);

legend({’Quadratic’, ’Sine’});

(Dr. Mihail) Plots February 17, 2015 19 / 24

Page 20: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

(Dr. Mihail) Plots February 17, 2015 20 / 24

Page 21: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Plot Properties

What about x and y-axis limits legend? In MATLAB:xlim([lower, upper]) and ylim([lower, upper]);. Example:

x = linspace(-3, 3, 100);

y1 = x.^2;y2 = sin(x);

figure(1);clf;hold on;

plot(x, y1, ’.g’);

plot(x, y2, ’--r’);

xlim([-0.2 1]);ylim([-0.2 1]);

xlabel(’x’); ylabel(’y’);title(’Quadratic and Sine’);

legend({’Quadratic’, ’Sine’});

(Dr. Mihail) Plots February 17, 2015 21 / 24

Page 22: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

(Dr. Mihail) Plots February 17, 2015 22 / 24

Page 23: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Two 1D Function Plots

Other plots

polar: polar plot using polar coordinates

bar, barh, bar3, bar3h, pie, pie3

use doc function name or your textbook to find out more aboutthem.

(Dr. Mihail) Plots February 17, 2015 23 / 24

Page 24: Plottingmypages.valdosta.edu/rpmihail/teaching/S17/CS1340/slides/plot.pdfPlots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability

Subplots

Dividing up the Figure window

Done using subplot(m, n, p)

Figure window split into a grid of size m by n, and p is the currentsubwindow, numbered from left to right, top to bottom

(Dr. Mihail) Plots February 17, 2015 24 / 24