Data Visualization

Created


Simple 2D Printing

plot(x,y) is a plotting command where x and y are vectors of equal length that give the x-coordinates and corresponding y-positions.

a = 0:0.1:10
b = a^2+3*a+5
plot(a,b)

To include multiple plots on the same graph you can greate many pairs of x and y values and plot them as

plot(x1, y1, x2, y2)

If you want to make a multi plot with a single vector of x-values and multiple y-values, put them on various rows of the same matrix

x1 = 0:0.1:10;
multiy(1,:) ax.^2 + 5
multiy(2, :) = -ax.^2 +3*ax
plot(x1, multiy)

Other plotting functions:

ezplot(function, [xmin, xmax])
fplot(function, [xmin xmax])

fplot has the advantage that it places more points near regions of high variation in the function.

Complex Data imaginary part is typically ignored, you must specify real() and imag() parts:

x = 0:0.01:6;
y = 3*exp(i*x)+2*exp(0.3*i*x); plot(x,[real(y); imag(y)])
legend('real','imaginary')

Polar Plots angle gives the phase angle, θ, of the complex number and abs gives the magnitude, r, where c+iy = rexp(iθ)

x = 0:0.01:6;  
y = 3*exp(i*x)+2*exp(0.3*i*x); polar(angle(y), abs(y))

x + i y = r exp(iθ)

x = 0:0.01:6;
y = 3exp(ix)+2exp(0.3i*x); polar(angle(y), abs(y))

Visual Stylization

Titles and Labels Title your graph using the function title('MY_TITLE') Label with xlabel('MY_LABEL') and ylabel('MY_LABEL') Add a grid using the grid function

Stylize Use stream modifiers to change font styles. Greek and mathematical symbols can be included.

Distinguish plots with line color, style, and marker style . Example:

plot(x, y, 'ro') % red, circle
plot(x, y, 'PropertyName', value)

Legends Include a legend with this command

legend(label1, label2)

Log Scales Plot on different axes with these commands

plot() % linear
semilogx() % x is logarithmic, y is linear
semilogy() % x is linear, y is logarithmic
loglog() % both x and y are logarithmic. 

Editing plots

v = axis sets the range of the axes axis([xmin xmax ymin ymax]) sets the axis limits by assigning a 4-element row vector to v above. axis equal sets increments to be equal on all axis axis square makes the current axis square axis on/off turns off and on all axes and labels.

Many Plots

hold on allows you to create multiple plots on the same axis which cause all additional plots to be laid on top of the existing plot. hold off reverts to normal behavior

Use figure(n) to create multiple figure windows.

Subplots To create one figure with many plots on them, use subplot(m, n, p). It divides the screen into m x n equal regions and creates axes at the p region as numbered from left to right, top to bottom.

3D Line Plots

Say you have some time dependent plots:

x(t) = exp(-0.2 t) cos(2 t)
y(t) = exp(-0.2 t) sin(2 t)

Plot this in 2D by:

t = 0:0.1:10;
x = exp(-0.2*t) .* cos(2*t); y = exp(-0.2*t) .* sin(2*t);
plot(x,y);

or in 3D by:

t = 0:0.1:10;  
x = exp(-0.2*t) .* cos(2*t); y = exp(-0.2*t) .* sin(2*t);
plot3(x, y, t);

A Mesh

Ccreate two matrices x and y where x will contain the x value of the grid point and y will contain the y value. Do this with ``[x,y]= meshgrid(xstart:xinc:xend, ystart:yinc:yend)Once the grid is made, you can compute a function of x and y withz = (x.^2, y.^2)` In Matlab:

[col, row] = meshgrid(-5:5, -5:5)
M = sqrt(row.^2 + col.^2);

z can be a 3D plot:

z = (x.^2 – y.^2) .* exp(-(x.^2 + y.^2));

Another example:

[x, y] = meshgrid(-4:0.1:4, -4:0.1:4);
z = (x.^2-y.^2).*exp(-(x.^2+y.^2));
figure(1)  
pcolor(x,y,z);

Views

pcolor(x,y,z) plots a pseudocolor plot contour(x,y,z); plots a contour contourf(x, y, z); %filled contour

Perspective views can be created with mesh, meshc, surf, surfc, surfl

Label contours with

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

Options Shade with these options:

shading faceted
shading flat
shading interp
colormap pink
colormap copper