octave graphicsdpo.cusat.ac.in/tutorial/octave/slide/plot.pdf · types of plots line plots pie...

Post on 07-Jul-2020

27 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Octave Graphics

Types of Plots

Line Plots Pie chart, bar plot, histograms Polar plot Contour plot Vector plot Surface, volumes, polygons Animation

Line/Symbol Plot

Line Plots

(1) plot [Line/Symbol Plot ]

Also called as X­Y plot where y varies with x. Syntax: plot(y) % y varies with x as index (1,2,3..)

plot(x,y) % y varies with x as solid line

plot(x,y,fmt) % fmt is line and color

specifications

Where 'x' is the data for X axis and 'y' is the dependent variable.

x = [0:5:100];

y = x;

plot(x, y)

Example ­ 1

x = [-100:5:100];

y = x.^2;

plot(x, y)

Example ­ 2

Example ­ 3

x = [0:0.01:10];

y = sin(x);

plot(x, y)

xlabel('x')

ylabel('Sin(x)')

title('Sin(x) Graph')

grid on

axis equal

Line/symbol specifications

Three options: Line style  Symbol color

Example

Additional options:

LineWidth — Specifies the width (in points) of the

Line.

MarkerSize — Specifies the size of the marker in

points (must be greater than 0).

Example

Adding Title & Axis Labels

Command Purpose Syntax

=============================================

title to add title title('text')

xlabel X axis label xlabel('text')

ylabel Y axis label ylabel('text')

Axis commands

axis equal : same scale

axis square : square plot

axis tight :

grid on : have grid

axis ( [xmin xmax ymin ymax] )

Multiple lines

x = [0 : 0.01: 10];

y = sin(x);

g = cos(x);

plot(x, y, x, g, '.-')

legend('Sin(x)', 'Cos(x)')

'subplot' : More plots in a page

The subplot command is used for creating several plots in a page.

Each plot is a subplot. Syntax:

subplot(m,n,p)

where m, n are number of rows and columns and p specifies the position of the respective plot.

Example: create 1x2 plotx=0:360;

y=sin(x*pi/180);

z=cos(x*pi/180);

subplot(1,2,1) % first position

plot(x,y)

subplot(1,2,2) % second position

plot(x,z)

Example: 4 plotssubplot(2,2,1)

plot(...)

subplot(2,2,2)

plot(...)

subplot(2,2,3)

plot(...)

subplot(2,2,4)

plot(...)

(2) plot3 [3D line plot]

Syntax:

plot3(X1,Y1,Z1,...)

plot3(X1,Y1,Z1,LineSpec,...)

plot3(...,'PropertyName',PropertyValue,...)

plot3(ax,...)

h = plot3(...)

Example

t = 0:pi/50:10*pi;

st = sin(t);

ct = cos(t);

figure

plot3(st,ct,t)

(3) loglog

Syntax:loglog(Y)

loglog(X1,Y1,...)

loglog(X1,Y1,LineSpec,...)

loglog(...,'PropertyName',PropertyValue,...)

loglog(ax,...)

h = loglog(...)

Example

x = logspace(-1,2);

y = exp(x);

loglog(x,y,'-s')

grid on

(4) semilogx

Syntax:

semilogx(Y)

semilogx(X1,Y1,...)

semilogx(X1,Y1,LineSpec,...)

semilogx(...,'PropertyName',PropertyValue,...)

semilogx(ax,...)

h = semilogx(...)

Example

x = 0:1000;

y = log(x);

figure

semilogx(x,y)

(5) semilogy

x = 0:0.1:10;

y = exp(x);

figure

semilogy(x,y)

(6) errorbar

errorbar(x,y,err) plots y versus x and draws a vertical error bar at each 

data point.

x = 1:10:100;

y = [20 30 45 40 60 65 80 75 95 90];

err = 8*ones(size(y));

errorbar(x,y,err)

Options

Horizontal errorbar:

errorbar(x, y, err, 'horizontal')

Vertical and Horizontal errorbar:

errorbar(x, y, err, 'both')

(2) Bar Plot

'bar' command

Example­1 (Only y)

Example­2 (x & y)

Example­3 (speficy bar width)

Example – 4 (groups)

Example – 5 (stacked)

Example – 6 (bar color)

'bar3' command

Draws a three­dimensional bar graph.

Syntax:bar3(Y)

bar3(x,Y)

bar3(...,width)

bar3(...,'style')

bar3(...,LineSpec)

bar3(axes_handle,...)

h = bar3(...)

Example

load count.dat

Y = count(1:10,:);

figure

bar3(Y)

title('Detached Style')

'barh' command

Plot bar graph horizontally

Syntax:barh(y)

barh(x,y)

barh(...,width)

barh(...,style)

barh(...,'bar_color')

barh(...,'PropertyName',PropertyValue,...)

barh(ax,...)

b = barh(...)

Example

Y = [57,91, ...281.4];

figure

barh(y)

'bar3h'

Histograms

Functions

histogram

x = randn(10000,1);

h = histogram(x)

'histogram2'

Pie Plot

Syntax: pie(X)

example

pie(X,explode)

example

pie(X,labels)

example

pie(X,explode,labels)

example

pie(ax,___)

example

p = pie(___)

X = [1 3 0.5 2.5 2];

pie(X)

explode = [0 1 0 1 0];

pie(X,explode)

pie3

Scatter Plot

Example: circles with different sizes

Example: filled

Polar Plot

Functions

polarplot

polarscatter

polarhistogram

Contour Plot

Functions

contour function

[X,Y,Z] = peaks;

figure

contour(X,Y,Z,20)

contourf function

Z = peaks(20);

contourf(Z,10)

clabel

[x,y,z] = peaks;

[C,h] = contour(x,y,z);

clabel(C,h)

Vector Plot

Functions

'feather' command

theta = -pi/2:pi/16:pi/2;

r = 2*ones(size(theta));

[u,v] = pol2cart(theta,r);

feather(u,v)

'quiver' function

[x,y] = meshgrid(0:0.2:2,0:0.2:2);

u = cos(x).*y;

v = sin(x).*y;

figure

quiver(x,y,u,v)

'compass' function

rng(0,'twister') % initialize random number generator

M = randn(20,20);

Z = eig(M);

figure

compass(Z)

streamline

Not yet implemented in octave

Animations

Functions

top related