欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python matplotlib库学习

程序员文章站 2024-01-15 19:32:40
...

python matplotlib库学习

1. 绘制3D图

#导入必要的库
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#创建画板及画布
fig = plt.figure()
ax = Axes3D(fig)
#设定x,y,z轴的值
x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
#生成网格数据
x,y = np.meshgrid(x,y)
#计算每个点对的长度
r = np.sqrt(x**2+y**2)
#计算z轴的高度
z = np.sin(r)
#plot_surface绘制三维图的函数
'''
rstride:行之间的网格跨度;
cstride:列之间的网格跨度;
rcount:行之间的间隔个数;(rcount,ccount不能与rstride,cstride同时出现)
ccount:列之间的间隔个数;
'''
ax.plot_surface(x,y,z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
#contourf投影函数
'''
zdir:代表从3D曲面到底面的投影,可以是x,y,z任意一个,代表投影到x,y,z平面
zdir='z',offset=-2代表投影到z=-2这一个平面上
'''
ax.contourf(x,y,z,zdir='z',offset=-2,cmap='rainbow')
#set_zlim设置z的维度,即z轴的取值范围
ax.set_zlim(-2,2)
plt.show()

相关标签: python