MATLAB Code

[X, Y] = meshgrid(-2:.2:2); Z = X .* exp(-X.^2 -Y.^2); surf(X,Y,Z);


In [6]:
%matplotlib inline

In [7]:
# Matplotlib code
from matplotlib import cm  #Color maps
from matplotlib import pyplot as plt  #The MATLAB-like interface to matplotlib
from mpl_toolkits.mplot3d import Axes3D #Need this for a '3d' projection
import numpy as np #NumPy is used for working with vectors

v = np.arange(-2, 2, 0.2)
X, Y = np.meshgrid(v, v) #In MATLAB, meshgrid(vector) is shorthand for meshgrid(vector, vector)
Z = X * np.exp(-X**2 -Y**2)

# Create a figure
fig = plt.figure(figsize=(10,10))
# Create 3D axes in the figure.
ax = fig.gca(projection='3d')
# Plot the surface.
surf = ax.plot_surface(X,Y,Z, cmap=cm.jet, rstride=1, cstride=1)
# Adjust the viewing angle.
ax.view_init(elev=20.0, azim=250.0)



In [8]:
def cylinder(r,n):
    '''
    Returns the unit cylinder that corresponds to the curve r.
    INPUTS:  r - a vector of radii
             n - number of coordinates to return for each element in r

    OUTPUTS: x,y,z - coordinates of points
    '''
    # ensure that r is a column vector
    r = np.atleast_2d(r)
    r_rows, r_cols = r.shape
    
    if r_cols > r_rows:
        r = r.T

    # find points along x and y axes
    points  = np.linspace(0, 2*np.pi,n+1)
    x = np.cos(points)*r
    y = np.sin(points)*r

    # find points along z axis
    rpoints = np.atleast_2d(np.linspace(0, 1, len(r)))
    z = np.ones((1,n+1))*rpoints.T
    
    return x,y,z

MATLAB Code

t = 0:pi/10:2*pi; [X,Y,Z] = cylinder(4*cos(t)) surf(X, Y, Z)


In [9]:
# Matplotlib code
from matplotlib import cm  #Color maps
from matplotlib import pyplot as plt  #The MATLAB-like interface to matplotlib
from mpl_toolkits.mplot3d import Axes3D #Need this for a '3d' projection
import numpy as np #NumPy is used for working with vectors

t = np.arange(0.0, 2.0*np.pi, np.pi/10.0)
X, Y, Z = cylinder(4.0 * np.cos(t), 20)

# Create a figure
fig = plt.figure(figsize=(10,10))
# Create 3D axes in the figure.
ax = fig.gca(projection='3d')

# Plot the surface.
surf = ax.plot_surface(X,Y,Z, cmap=cm.jet, rstride=1, cstride=1)
# Adjust the viewing angle.
ax.view_init(elev=20.0, azim=250.0)



In [10]:
# Matplotlib code
from matplotlib import cm  #Color maps
from matplotlib import pyplot as plt  #The MATLAB-like interface to matplotlib
import mpl_toolkits.mplot3d.axes3d as p3
import numpy as np #NumPy is used for working with vectors

t = np.arange(0.0, 2.0*np.pi, np.pi/10.0)
#t = np.linspace(0.0, 2.0*np.pi, np.pi/10.0)
X, Y, Z = cylinder(4.0 * np.cos(t), 20)

# Create a figure
fig = plt.figure(figsize=(10,10))
# Create 3D axes in the figure (alternative syntax).
ax = p3.Axes3D(fig)
# Plot wireframe
ax.plot_wireframe(X,Y,Z)

# Adjust the viewing angle.
ax.view_init(elev=20.0, azim=250.0)



In [11]:
from matplotlib.cm import get_cmap  #Color maps

# Generate colors.
def colorgen(num_colors=22, colormap='Paired'):
    cm = get_cmap(colormap)
    colors = [cm(1.*i/num_colors) for i in range(num_colors)]
    return colors

MATLAB Code

[X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R surf(X,Y,Z);


In [17]:
from matplotlib import cm  #Color maps
from matplotlib import pyplot as plt  #The MATLAB-like interface to matplotlib
from mpl_toolkits.mplot3d import Axes3D #Need this for a '3d' projection
import numpy as np #NumPy is used for working with vectors

v = np.arange(-8.0, 8.0, 0.5)
X, Y = np.meshgrid(v, v) #In MATLAB, meshgrid(vector) is shorthand for meshgrid(vector, vector)
R = np.sqrt(X**2 + Y**2) + np.finfo(float).eps
Z = np.sin(R)/R

# Create a figure
fig = plt.figure(figsize=(10,10))
# Create 3D axes in the figure.
ax = fig.gca(projection='3d')
# Plot.
o = ax.plot_wireframe(X,Y,Z, colors=colorgen(colormap='jet'))
# Cover the mesh with a surface.
surf = ax.plot_surface(X,Y,Z, cmap=cm.jet, rstride=1, cstride=1)


MATLAB Code

[X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; mesh(Z);


In [16]:
from matplotlib import cm  # Color maps
from matplotlib import pyplot as plt  # The MATLAB-like interface to matplotlib
from mpl_toolkits.mplot3d import Axes3D # Need this for a '3d' projection
import numpy as np # NumPy is used for working with vectors

v = np.linspace(-8.0, 8.0, num=33)
X, Y = np.meshgrid(v, v) # In MATLAB, meshgrid(vector) is shorthand for meshgrid(vector, vector)
R = np.sqrt(X**2 + Y**2) + np.finfo(float).eps
Z = np.sin(R)/R

# MATLAB mesh(Z) => mesh(X, Y, Z) where X = [1...n], Y = [1...m] where Z.shape == (m,n)
# MATLAB does a lot of implicit broadcasting.
# Here, we explicitly create our vectors (in this case, since m==n it is just 1 vector used 2x).
# Then we use meshgrid() to combine them.
vecx = np.linspace(1,33,num=33)
vecy = vecx
x,y = np.meshgrid(vecx,vecy)

# Create a figure
fig = plt.figure(figsize=(10,10))
# Create 3D axes in the figure.
ax = fig.gca(projection='3d')

# Plot.
o = ax.plot_wireframe(x, y, Z, colors=colorgen(colormap='jet'))


MATLAB Code

t = 0:pi/10:2*pi; [X,Y,Z] = cylinder(4*cos(t)); subplot(2,2,1); mesh(X); title('X'); subplot(2,2,2); mesh(Y); title('Y'); subplot(2,2,3); mesh(Z); title('Z'); subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');


In [15]:
from matplotlib import cm  #Color maps
from matplotlib import pyplot as plt  #The MATLAB-like interface to matplotlib
from mpl_toolkits.mplot3d import Axes3D #Need this for a '3d' projection
import numpy as np #NumPy is used for working with vectors

def plot_meshgrid(Z, ax=None, **kwds):
    if ax is None:
        ax = gca()
    n, m = Z.shape
    vecx = np.linspace(1,m,num=m)
    vecy = np.linspace(1,n,num=n)
    x,y = np.meshgrid(vecx,vecy)
    ax.plot_wireframe(x,y,Z, **kwds)

#v = np.linspace(-8.0, 8.0, num=33)
t = np.arange(0.0, 2.0*np.pi, np.pi/10.0)
X, Y, Z = cylinder(4.0 * np.cos(t), 20)

# Create a figure
fig = plt.figure(figsize=(10,10))

# Create 3D axes in the figure.
#ax = fig.gca(projection='3d')
# Plot.
ax = fig.add_subplot(221, projection='3d')
plot_meshgrid(X, ax=ax, colors=colorgen(colormap='jet'))
ax.set_title('X')

ax = fig.add_subplot(222, projection='3d')
plot_meshgrid(Y, ax=ax, colors=colorgen(colormap='jet'))
ax.set_title('Y')

ax = fig.add_subplot(223, projection='3d')
plot_meshgrid(Z, ax=ax, colors=colorgen(colormap='jet'))
ax.set_title('Z')

ax = fig.add_subplot(224, projection='3d')
o = ax.plot_wireframe(X,Y,Z, colors=colorgen(colormap='jet'))
o = ax.set_title('X,Y,Z')