plot_wireframe
¶To draw a 3D graph, use the parameters of plt.subplots()
as subplot_kw = { 'projection' : '3d' }
.
# 11-1-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def func(x, y):
z = x * x + y * y
return z
x = np.arange(-3, 3, 0.1)
y = np.arange(-3, 3, 0.1)
X, Y = np.meshgrid(x, y) # Generate 2D grid coordinates
Z = func(X, Y)
fig, ax = plt.subplots(1, 1, figsize=(8,6), subplot_kw={'projection' : '3d'})
ax.plot_wireframe(X, Y, Z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel(r'$f(x,y)$')
plt.show()
Generate 2D grid coordinates.
# 11-1-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
def func(x, y):
z = x * x + y * y
return z
x = np.arange(-3, 3, 0.1)
y = np.arange(-3, 3, 0.1)
X, Y = np.meshgrid(x, y) # create 2D grid coordinates
Z = func(X, Y)
fig, ax = plt.subplots(figsize=(8,6)) # generate Figure object
ax2 = axes3d.Axes3D(fig) # generate Axes3D object in Figure
ax2.plot_wireframe(X, Y, Z)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel(r'$f~(x,y)$')
plt.show()
plot_surface
¶# 11-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
def func(x, y):
z = x * x + y * y
return z
x = np.arange(-3, 3, 0.1)
y = np.arange(-3, 3, 0.1)
X, Y = np.meshgrid(x, y) # generate 2D grid coordinates
Z = func(X, Y)
fig, ax = plt.subplots(figsize=(8,6)) # generate Figure object
ax2 = axes3d.Axes3D(fig) # generate Axes3D object in Figure
ax2.plot_surface(X, Y, Z)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel(r'$f~(x,y)$')
plt.show()
Axes3D.plot
¶# source code 11-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
# prepare data
dt = 0.001
ts = np.arange(0.001, 20, dt)
def lorenz(x, y, z, dt, p=10, r=28, b=8/3):
dx = dt * (p * (-x + y))
dy = dt * (-x * z + r * x - y)
dz = dt * (x * y - b * z)
return dx, dy, dz
def rungeKutta(x, y, z, dt, f):
x0, y0, z0 = f(x, y, z, dt)
x1, y1, z1 = f(x+x0/2., y+y0/2., z+z0/2., dt)
x2, y2, z2 = f(x+x1/2., y+y1/2., z+z1/2., dt)
x3, y3, z3 = f(x+x2, y+y2, z+z2, dt)
xn = x + (x0 + 2. * (x1+x2) + x3)/6.
yn = y + (y0 + 2. * (y1+y2) + y3)/6.
zn = z + (z0 + 2. * (z1+z2) + z3)/6.
return xn, yn, zn
x, y, z = 1., 1., 1.
xs, ys, zs = [], [], []
for t in ts:
x, y, z = rungeKutta(x, y, z, dt, lorenz)
xs.append(x)
ys.append(y)
zs.append(z)
fig = plt.figure()
ax = axes3d.Axes3D(fig) # generate Axes3D in Figure
# Draw in 3D
ax.plot(xs, ys, zs, label='parametric curve')
ax.legend() # legend
plt.show()
bar3d
¶Draw the histogram generated with 2D data in 3D.
# sample code 11-4-1
import numpy
_x = np.arange(3)
_y = np.arange(5)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
print(_x, _y)
print(_xx, _yy)
print(x, y)
[0 1 2] [0 1 2 3 4] [[0 1 2] [0 1 2] [0 1 2] [0 1 2] [0 1 2]] [[0 0 0] [1 1 1] [2 2 2] [3 3 3] [4 4 4]] [0 1 2 0 1 2 0 1 2 0 1 2 0 1 2] [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4]
# sample code 11-4-2
# Draw a 2D histogram as a 3D bar graph
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
top = x + y
bottom = np.zeros_like(top)
width = depth = 0.5
top2 = np.abs(np.sin(x+y) + np.cos(x-y))
fig = plt.figure(figsize=(8,6))
ax1 = fig.add_subplot(121, projection='3d')
ax1.bar3d(x, y, bottom, width, depth, top)
ax1.set_title('Shaded')
ax2 = fig.add_subplot(122, projection='3d')
ax2.bar3d(x, y, bottom, width, depth, top2)
plt.show()