Plotting Dr. Mihail September 25, 2018 (Dr. Mihail) Plots - - PowerPoint PPT Presentation

plotting
SMART_READER_LITE
LIVE PREVIEW

Plotting Dr. Mihail September 25, 2018 (Dr. Mihail) Plots - - PowerPoint PPT Presentation

Plotting Dr. Mihail September 25, 2018 (Dr. Mihail) Plots September 25, 2018 1 / 24 Plots Visualize One of the most important uses of packages like MATLAB is their rich data visualization capability. Visualizing data often yields insights


slide-1
SLIDE 1

Plotting

  • Dr. Mihail

September 25, 2018

(Dr. Mihail) Plots September 25, 2018 1 / 24

slide-2
SLIDE 2

Plots

Visualize

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

(Dr. Mihail) Plots September 25, 2018 2 / 24

slide-3
SLIDE 3

Plots

Figures

Graphic visulizations in MATLAB are produced in Figure windows. The MATLAB 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 Figure windows opened simultaneously. Switching between them is done using the figure() function.

(Dr. Mihail) Plots September 25, 2018 3 / 24

slide-4
SLIDE 4

Figure Window

(Dr. Mihail) Plots September 25, 2018 4 / 24

slide-5
SLIDE 5

Clearing Figure Window

To clear, use the clf command. Example: >> figure(1) >> clf >>

(Dr. Mihail) Plots September 25, 2018 5 / 24

slide-6
SLIDE 6

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 September 25, 2018 6 / 24

slide-7
SLIDE 7

1D Function Plot

The result:

(Dr. Mihail) Plots September 25, 2018 7 / 24

slide-8
SLIDE 8

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 September 25, 2018 8 / 24

slide-9
SLIDE 9

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 September 25, 2018 9 / 24

slide-10
SLIDE 10

1D Function Plot

The choppy result:

(Dr. Mihail) Plots September 25, 2018 10 / 24

slide-11
SLIDE 11

Simple 2D Plots

Multiple graphs same figure

To graph multiple plots on the same figure, use hold on;. This will hold

  • n 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 September 25, 2018 11 / 24

slide-12
SLIDE 12

Two 1D Function Plots

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

(Dr. Mihail) Plots September 25, 2018 12 / 24

slide-13
SLIDE 13

Two 1D Function Plots

Each of the two functions has 100 data points. We can change the default behavior of plot using the following syntax plot(x, y, LineSpec), where LineSpec is a string that sets the line style, marker symbol and

  • color. For more info, type >> doc plot.

(Dr. Mihail) Plots September 25, 2018 13 / 24

slide-14
SLIDE 14

LineSpec

plot(x, y, LineSpec) Line style, marker symbol, and color, specified as a string. The elements of the string can appear in any order, and you can omit one or more options from the string specifier. If you omit the line style and specify the marker character, 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 are plotted using the specified color. If you specify a marker type or line style and do not specify a color, then MATLAB cycles through the predefined color order. Example: Example: ’--or’ is a red dashed line with circle markers

(Dr. Mihail) Plots September 25, 2018 14 / 24

slide-15
SLIDE 15

Simple 2D Plots

Multiple graphs same figure

Let’s change our previous example, make quadratic dotted green and the sine 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 September 25, 2018 15 / 24

slide-16
SLIDE 16

Two 1D Function Plots

(Dr. Mihail) Plots September 25, 2018 16 / 24

slide-17
SLIDE 17

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 September 25, 2018 17 / 24

slide-18
SLIDE 18

Two 1D Function Plots

(Dr. Mihail) Plots September 25, 2018 18 / 24

slide-19
SLIDE 19

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 September 25, 2018 19 / 24

slide-20
SLIDE 20

Two 1D Function Plots

(Dr. Mihail) Plots September 25, 2018 20 / 24

slide-21
SLIDE 21

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 September 25, 2018 21 / 24

slide-22
SLIDE 22

Two 1D Function Plots

(Dr. Mihail) Plots September 25, 2018 22 / 24

slide-23
SLIDE 23

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 about them.

(Dr. Mihail) Plots September 25, 2018 23 / 24

slide-24
SLIDE 24

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 current subwindow, numbered from left to right, top to bottom

(Dr. Mihail) Plots September 25, 2018 24 / 24