import numpy as np
import matplotlib.pyplot as plt
# 不用打imshow也能显示
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
3D
fig = plt.figure()
# 相当于把2维图放到Axes3D渲染
ax = Axes3D(fig)
x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
# 形成矩阵
X,Y = np.meshgrid(x,y)
Z = np.sin(np.sqrt(X**2+Y**2))
# 画面, rstride是密度相关的
# 注意画面的时候,z需要矩阵
ax.plot_surface(x,y,Z, rstride=1, cstride=1, cmap='rainbow')
fig = plt.figure()
# 需要加一个projection
ax = fig.add_subplot(211, projection='3d')
x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
z = np.sin(np.sqrt(x**2+y**2))
# 画线只需要三维坐标
ax.plot(x,y,z)
[]
fig = plt.figure()
# 需要加一个projection
ax = fig.add_subplot(211, projection='3d')
x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
z = np.sin(np.sqrt(x**2+y**2))
# 画线只需要三维坐标
ax.scatter(x,y,z)
# 可以指定观察角度
#ax.view_init(20,20)
fig = plt.figure(figsize=(8,10))
# 需要加一个projection
ax = fig.add_subplot(211, projection='3d')
ax.set_zlim([0,4])
for i in range(3):
x = np.arange(0,4,0.25)
y = np.arange(0,4,0.25)
# zdir表示地三维的
ax.bar(x,y,zs=i*10, zdir='y', alpha=0.5)