首页 > 解决方案 > 设置固定颜色条值

问题描述

我正在绘制一些按时间步长动画的节点数据,如下所示:

fig, ax = plt.subplots()
fig.tight_layout()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

def animate(i):
    ax.cla()
    plt.cla()
    ax.set_aspect('equal', 'box')
    c = ax.tricontourf(triang, z[:, i], 10, cmap='plasma')
    c.set_clim(np.min(z), np.max(z))
    plt.colorbar(c, cax=cax)

anim = FuncAnimation(fig, animate, interval=100, frames=nt)

其中 z - 是nnodes x number_of_timesteps节点值矩阵。但正如您在下图中看到的那样,颜色条的范围和值似乎并没有固定。我的意思是分配给特定颜色的值似乎是固定的,但颜色图例会及时变化。我认为c.set_clim(np.min(z), np.max(z))应该修复它,因为它在每个时间步从整组数据中获取最小和最大节点值,但显然它并没有修复颜色条。有办法解决吗?

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: matplotlibcolorbarclim

解决方案


您每次都会得到不同的颜色条,因为您没有指定轮廓的级别。尝试:

c = ax.tricontourf(triang, z[:, i], 10, cmap='plasma', vmin=-1, vmax=1, levels=np.arange(-1, 1.02, 0.1))


推荐阅读