NOTE: The Jupyter notebook requires some more work to display MATLAB plots:
%matplotlib inline
import pylab as py
import numpy as np
Now we are ready to do some plotting with MATLAB - just note that functions are appended with np & py and comments are now # instead of %.
# define some vectors for x and y
x = (1,2,3,4,5,6,7,8,9);
y = (-1,3,-2,7,8,9,3,6,8);
# plot it! Remember that normally in MATLAB it would just be plot(x,y)
py.plot(x,y)
[<matplotlib.lines.Line2D at 0x10f936d90>]
# we can also use linspace to auto-generate a range of numbers
x = np.linspace(0,20,1000)
y = np.sin(x)
py.plot(x,y)
[<matplotlib.lines.Line2D at 0x10fabffd0>]
# we can use xlim() and ylim() to define the limits of the plot
py.plot(x, y)
py.xlim(5, 15)
py.ylim(-1.2, 1.2)
(-1.2, 1.2)
# we can label axis' and title the plot
py.xlabel('X-Axis')
py.ylabel('Y-Axis')
py.title('My Matlab Plot')
py.plot(x, y)
[<matplotlib.lines.Line2D at 0x10fdedb90>]
# LaTeX is also supported
y = np.sin(2*np.pi*x)
py.plot(x, y)
py.title(r'$\sin(2 \pi x)$') # the `r` before the string indicates a "raw string"
<matplotlib.text.Text at 0x10ff4bc90>
# the color of the plot can be changed
y = np.sin(2*np.pi*x)
py.plot(x, y, 'r')
py.title(r'$\sin(2 \pi x)$')
<matplotlib.text.Text at 0x1100eb990>
'r' = red 'g' = green 'b' = blue 'c' = cyan 'm' = magenta 'y' = yellow 'k' = black 'w' = white
'-' = solid '--' = dashed ':' = dotted '-.' = dot-dashed '.' = points 'o' = filled circles '^' = filled triangles
# multiple plots on the same chart with a legend
x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
py.plot(x, y1, '-b', label='sine')
py.plot(x, y2, '-r', label='cosine')
py.legend(loc='upper right')
py.ylim(-1.5, 2.0)
(-1.5, 2.0)
# more customization
x1 = np.linspace(0, 10, 20)
y1 = np.sin(x1)
x2 = np.linspace(0, 10, 1000)
y2 = np.sin(x2)
py.ylim(-1.5, 2.0)
py.plot(x1, y1, 'bo', label='sampled')
py.plot(x2, y2, ':k', label='continuous')
py.legend()
<matplotlib.legend.Legend at 0x1105bf450>
# multiple plots are achieved with the subplot() command
x = np.linspace(-5,5);
y1 = np.sin(x);
py.subplot(1,4,1)
py.plot(x,y1)
py.title('First subplot')
y2 = np.sin(2*x);
py.subplot(1,4,2)
py.plot(x,y2)
py.title('Second subplot')
y3 = np.sin(4*x);
py.subplot(1,4,3)
py.plot(x,y3)
py.title('Third subplot')
y4 = np.sin(6*x);
py.subplot(1,4,4)
py.plot(x,y4)
py.title('Fourth subplot')
# by changing to subplot(2,2,n) we would get a 2x2 grid of plots
<matplotlib.text.Text at 0x1108d3c90>
Command | Result |
---|---|
plot(x,y) | creates an Cartesian plot of the vectors x & y |
plot(y) | creates a plot of y vs. the numerical values of the elements in the y-vector |
semilogx(x,y) | plots log(x) vs y |
semilogy(x,y) | plots x vs log(y) |
loglog(x,y) | plots log(x) vs log(y) |
grid | creates a grid on the graphics plot |
title('text') | places a title at top of graphics plot |
xlabel('text') | writes 'text' beneath the x-axis of a plot |
ylabel('text') | writes 'text' beside the y-axis of a plot |
text(x,y,'text') | writes 'text' at the location (x,y) |
text(x,y,'text','sc') | writes 'text' at point x,y assuming lower left corner is (0,0) and upper right corner is (1,1) |
gtext('text') | writes text according to placement of mouse |
hold on | maintains the current plot in the graphics window while executing subsequent plotting commands |
hold off | turns OFF the 'hold on' option |
polar(theta,r) | creates a polar plot of the vectors r & theta where theta is in radians |
bar(x) | creates a bar graph of the vector x (Note also the command stairs(y)) |
bar(x,y) | creates a bar-graph of the elements of the vector y, locating the bars according to the vector elements of 'x' (Note also the command stairs(x,y)) |
hist(x) | creates a histogram - this differs from the bargraph in that frequency is plotted on the vertical axis |
mesh(z) | creates a surface in xyz space where z is a matrix of the values of the function z(x,y). z can be interpreted to be the height of the surface above some xy reference plane |
surf(z) | similar to mesh(z), only surface elements depict the surface rather than a mesh grid |
contour(z) | draws a contour map in xy space of the function or surface z |
meshc(z) | draws the surface z with a contour plot beneath it |
meshgrid [X,Y]=meshgrid(x,y) | transforms the domain specified by vectors x and y into arrays X and Y that can be used in evaluating functions for 3D mesh/surf plots |
sends the contents of graphics window to printer | |
print filename -dps | writes the contents of current graphics to 'filename' in postscript format |