matplotlib - 设置固定颜色条值
问题描述
我正在绘制一些按时间步长动画的节点数据,如下所示:
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))
应该修复它,因为它在每个时间步从整组数据中获取最小和最大节点值,但显然它并没有修复颜色条。有办法解决吗?
解决方案
您每次都会得到不同的颜色条,因为您没有指定轮廓的级别。尝试:
c = ax.tricontourf(triang, z[:, i], 10, cmap='plasma', vmin=-1, vmax=1, levels=np.arange(-1, 1.02, 0.1))
推荐阅读
- android - React Native 卡在闪屏上
- python - Chrome Web 驱动程序返回无效的 Chrome 版本
- asp.net - 修改 ID 时,产品名称在 URL 中没有更改 ASP.Net Webform
- php - 如何在 laravel 8 中获取登录的用户 ID
- c# - type.GenericTypeArguments 的意外结果
- azure - Azure IOT HUB 诊断日志仅在信息级别显示设备连接日志,而不是任何错误级别日志
- oracle - Hibernate 原生查询
- c++ - 如何将编译选项添加到 CMake FetchContent 依赖项?
- jasper-reports - 如果数据超过一页,isPrintInFirstWholeBand="true" 属性将不起作用
- postgresql - Linux Mint 20.2:E:包 'pgadmin4' 没有安装候选