Matplotlib动画中的配色栏
我想在一些数据中添加一个配色栏以制作动画。
但是,我继续在图中创建新的颜色条,不知道如何删除它们。
可再现的例子是:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import cm
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, projection='3d', proj_type='ortho')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
data = [X, Y, Z]
def myPlot(ax, data):
surf = ax.plot_surface(*data, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
def anime(i, ax, data):
ax.cla()
data[0] += 0.1
myPlot(ax, data)
animation = FuncAnimation(
fig,
anime,
frames=range(len(X)),
fargs=(ax, data)
)
如何在动画中只保留一个配色杆?
亲切的问候
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须为配色栏创建一个轴:
并将其作为配色栏定义中的参数传递:
完整代码
动画
You have to create an axis for the colorbar:
and pass it as a parameter in the colorbar definition:
Complete Code
Animation