Wol*_*ury 3 python matplotlib colorbar
底部的代码完全符合我的要求,但仅限于matplotlib至少低于 3.3.4 的版本。对于此版本 3.3.4,我收到以下错误消息:
AttributeError: 'ColorBar' object has no attribute 'set_clim'
Run Code Online (Sandbox Code Playgroud)
因此,我试图找出如何在今天的版本中执行此操作,但失败了。那么,如何更改新版本中图像和 Colobar 的色阶呢?
工作代码(在2.2.2中测试):
import matplotlib.pyplot as plt
import numpy as np
import time
x = np.linspace(0, 10, 100)
y = np.cos(x)
y = y.reshape(10,10)
plt.ion()
figure = plt.figure()
line1 = plt.imshow(y)
cbar = plt.colorbar(line1)
for p in range(100):
updated_y = np.random.randint(0,10)*np.cos(x-0.05*p).reshape(10,10)
line1.set_data(updated_y)
cbar.set_clim(vmin=np.min(updated_y),vmax=np.max(updated_y)) #this line creates the error
cbar.draw_all()
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
我在以下帖子中找到了 @Trenton McKinneys 提供的链接的解决方案: Merk 的问题。
解决代码:
import matplotlib.pyplot as plt
import numpy as np
import time
x = np.linspace(0, 10, 100)
y = np.cos(x)
y = y.reshape(10,10)
plt.ion()
figure = plt.figure()
line1 = plt.imshow(y)
cbar = plt.colorbar(line1)
for p in range(100):
updated_y = np.random.randint(0,10)*np.cos(x-0.05*p).reshape(10,10)
line1.set_data(updated_y)
#cbar.set_clim(vmin=np.min(updated_y),vmax=np.max(updated_y)) #this line creates the error
cbar.mappable.set_clim(vmin=np.min(updated_y),vmax=np.max(updated_y)) #this works
cbar.draw_all()
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5264 次 |
| 最近记录: |