Matplotlib绘制三维图

简单的绘图示例代码:

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
# ax = fig.add_subplot(321, projection='3d')  # 321代表将整张图平均分为3*2=6份(上下,左中右),该图占第一份(左上角)
ax = plt.axes(projection='3d')
# 三维线的数据
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')
# 三维散点的数据
zdata = 15 * np.random.random(100)
print("zdata: ", zdata.shape)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')

plt.savefig('3d_img.png')
plt.show()

Matplotlib绘制三维图_第1张图片

详细介绍可参考:Python三维绘图–Matplotlib

你可能感兴趣的:(基础知识,python,matplotlib)