python bar函数默认颜色_python – 如何在matplotlib中为colorbar设置动画

我有一个动画,其中数据的范围变化很??大.我想有一个跟踪数据的最大值和最小值的颜色条(即我希望它不被修复).问题是如何做到这一点.

理想情况下,我希望颜色条在自己的轴上.

我尝试了以下四件事

1.天真的方法

问题:新的颜色条是每帧的绘图

#!/usr/bin/env python

"""

An animated image

"""

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

fig = plt.figure()

ax = fig.add_subplot(111)

def f(x, y):

return np.exp(x) + np.sin(y)

x = np.linspace(0, 1, 120)

y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

frames = []

for i in range(10):

x += 1

curVals = f(x, y)

vmax = np.max(curVals)

vmin = np.min(curVals)

levels = np.linspace(vmin, vmax, 200, endpoint = True)

frame = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)

cbar = fig.colorbar(frame)

frames.append(frame.collections)

ani = animation.ArtistAnimation(fig, frames, blit=False)

plt.show()

2.添加到图像

将上面的for循环更改为

initFrame = ax.contourf(f(x,y))

cbar = fig.colorbar(initFrame)

for i in range(10):

x += 1

curVals = f(x, y)

vmax = np.max(curVals)

vmin = np.min(curVals)

levels = np.linspace(vmin, vmax, 200, endpoint = True)

frame = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)

cbar.set_clim(vmin = vmin, vmax = vmax)

cbar.draw_all()

frames.append(frame.collections + [cbar])

问题:这引起了提升

AttributeError: 'Colorbar' object has no attribute 'set_visible'

3.在自己的轴上绘图

问题:颜色栏未更新.

#!/usr/bin/env python

"""

An animated image

"""

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

fig = plt.figure()

ax1 = fig.add_subplot(121)

ax2 = fig.add_subplot(122)

def f(x, y):

return np.exp(x) + np.sin(y)

x = np.linspace(0, 1, 120)

y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

frames = []

for i in range(10):

x += 1

curVals = f(x, y)

vmax = np.max(curVals)

vmin = np.min(curVals)

levels = np.linspace(vmin, vmax, 200, endpoint = True)

frame = ax1.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)

cbar = fig.colorbar(frame, cax=ax2) # Colorbar does not update

frames.append(frame.collections)

ani = animation.ArtistAnimation(fig, frames, blit=False)

plt.show()

2和4的组合.

问题:颜色条是不变的.

一个类似的问题是here发布,但看起来OP对固定的颜色条感到满意.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值