首页 > 技术文章 > 【Python】利用Python绘制3D图表

cloudsir 2021-08-04 17:35 原文

代码

'''
Author: CloudSir
Date: 2021-07-28 10:57:47
LastEditTime: 2021-08-04 17:33:07
LastEditors: CloudSir
Description: python绘制3D图表
https://github.com/cloudsir
'''
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文
plt.rcParams['axes.unicode_minus'] = False    # 用来正常显示负号
 
fig = plt.figure()
ax = Axes3D(fig) # 设置3D绘图区
'''在matplotlib 3.5版本中,要用下面的方式设置3d绘图区
ax = plt.axes(projection='3d')'''

x=np.arange(0,11,1)
y=np.arange(0,11,1)

X, Y = np.meshgrid(x, y) # 创建网格
Z = X + Y
ax.set_xlabel("x轴")
ax.set_ylabel("y轴")
ax.set_zlabel("z轴")
ax.plot_surface(X, Y, Z, cmap='rainbow') # 绘制3d曲面
ax.scatter3D(X, Y, Z) # 绘制3D点图

plt.show()

运行结果

推荐阅读