Matplotlib入门:3D图

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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 = np.sin(R)
"""1.3D曲面图"""
fig1 = plt.figure(num=1)
ax = plt.axes(projection='3d')
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow')
ax.set_zlim(-2,2)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3Dsurface')
# zdir为从哪一轴进行投影
# offset为投影到对应轴的什么位置
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')

"""2.3D线图"""
z = np.linspace(0, 1, 100)
x = z * np.sin(20 * z)
y = z * np.cos(20 * z)
fig2 = plt.figure(num=2)
ax = plt.axes(projection='3d')
ax.plot3D(x,y,z,'red')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3Dline')

"""3.3D散点图"""
fig3 = plt.figure(num=3)
ax = plt.axes(projection='3d')
ax.scatter3D(x,y,z,s=50,c=x+y,cmap='rainbow')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3Dscatter')

"""4.3D等高线图"""
fig4 = plt.figure(num=4)
ax = plt.axes(projection='3d')
ax.contour3D(X,Y,Z,50,cmap='binary')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3Dcontour')


plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值