如何在 Matplotlib 中设置颜色条的动画?
要在 matplotlib 中设置颜色条的动画,我们可以采用以下步骤:
设置图片大小并调整子图之间和周围的边距。
创建新的图片或激活现有的图片。
将 '~.axes.Axes' 添加到图片作为子图的一部分。
根据预先存在的坐标轴(即 ax 对象)实例化 Divider 并返回指定单元格的新坐标轴定位器。
在指定 *position* 处创建坐标轴,其 height(或 width)与主坐标轴相同。
使用 numpy 创建随机数据。
使用 imshow() 方法绘制随机数据。
设置图片标题。
对颜色映射表进行初始化。
要设置颜色条的动画,请使用 animate() 方法。
要显示图片,请使用 show() 方法。
示例
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')
data = np.random.rand(5, 5)
im = ax.imshow(data)
cb = fig.colorbar(im, cax=cax)
tx = ax.set_title('Frame 0')
cmap = ["copper", 'RdBu_r', 'Oranges', 'cividis', 'hot', 'plasma']
def animate(i):
cax.cla()
data = np.random.rand(5, 5)
im = ax.imshow(data, cmap=cmap[i%len(cmap)])
fig.colorbar(im, cax=cax)
tx.set_text('Frame {0}'.format(i))
ani = animation.FuncAnimation(fig, animate, frames=10)
plt.show()输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP