matplotlib 3D图&matplotlib subplot&matplotlib动态图

绘制3D图

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)

plt.show()

在这里插入图片描述

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(x,y)#x,y传入网格
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

#画三D图
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
#cmap设置图片颜色,rstride,cstride代表x和y的方向

plt.show()

在这里插入图片描述

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(x,y)#x,y传入网格
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

#画三D图
ax.plot_surface(X,Y,Z,rstride=3,cstride=3,cmap=plt.get_cmap('rainbow'))
#cmap设置图片颜色,rstride,cstride代表x和y的方向

plt.show()

在这里插入图片描述

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(x,y)#x,y传入网格
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

#画三D图
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
#cmap设置图片颜色,rstride,cstride代表x和y的方向

ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')
#zdir代表映射的方向,为z方向,offset代表映射的一个位置,到-2这个位置
ax.set_zlim(-2,2)
#设置z轴范围

plt.show()

在这里插入图片描述
这个图若是只这样看,看得不全面

若是安装了anaconda电脑上会有自带的ipython
在这里插入图片描述
在这里插入图片描述
将代码输入进去
在这里插入图片描述
按shift+回车键执行
在这里插入图片描述
此时可以用鼠标随意自由拉动来观察图像全貌
在这里插入图片描述
subplot

import matplotlib.pyplot as plt
import numpy as np
plt.figure()
plt.subplot(2,2,1)
#在一个图片中创建2行2列一个小的绘图,1代表它在这个图像中的第一个位置
plt.plot([0,1],[0,1])
#绘一条从0,0到1,1的直线

plt.subplot(2,2,2)
plt.plot([0,1],[0,1])

plt.subplot(223)#逗号去掉也可
plt.plot([0,1],[0,1])

plt.subplot(224)
plt.plot([0,1],[0,1])

plt.show()

在这里插入图片描述

plt.figure()
plt.subplot(2,1,1)
#在一个图片中创建2行1列一个小的绘图,1代表它在这个图像中的第一个位置
plt.plot([0,1],[0,1])
#绘一条从0,0到1,1的直线

plt.subplot(2,3,4)
#因为第一个已经占据了第一列,所以第二张图排到了第4个位置
plt.plot([0,1],[0,1])

plt.subplot(235)#逗号去掉也可
plt.plot([0,1],[0,1])

plt.subplot(236)
plt.plot([0,1],[0,1])

plt.show()

在这里插入图片描述
动态图

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation
fig,ax = plt.subplots()

x = np.arange(0,2*np.pi,0.01)
#pi就是0.3415926那个
line, = ax.plot(x,np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10))#y值的改变
    return line,


#初始值,动态图最开始的那张图
def init():
    line.set_ydata(np.sin(x))
    return line,

#画动态图
ani = animation.FuncAnimation(fig=fig,func=animate,init_func=init,interval=20)
#intervel表示间隔是20ms

plt.show()

在这里插入图片描述
这里的图无法动起来,所以我们还是要打开ipython
在这里插入图片描述
一个动态的不停地绘制正弦曲线的图
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值