Skip to main content
 首页 » 编程设计

python之创建动画时重复的颜色条

2025年05月04日8JustinYoung

我是 matplotlib 的新手。当我用循环生成一系列 contourf 图时,matplotlib 创建了 n 个重复的颜色条(颜色条具有固定范围)

我必须为每个循环销毁旧的颜色条,如果是这样我该怎么做?

更新:

好的,但是如果我为 contourf 图生成颜色条。我如何确保包括整个比例范围。我试过使用 "clip=False"& extend='both'

c_scale = matplotlib.colors.Normalize(vmin=0, vmax=1,clip=False) 
cbar = plt.colorbar(cmap=plt.cm.jet,norm=c_scale,shrink=0.5,extend='both') 

请您参考如下方法:

contourf 不会生成颜色条,颜色条会。

如果颜色条有一个固定的范围,你可以在开始时调用 colorbar() 一次,然后只绘制你的新 contourf 而无需调用 colorbar()

一个例子:

#In which we make contourplots with just one colorbar 
 
import matplotlib.pyplot as plt 
import numpy as np 
 
plt.figure(1) 
plt.clf() 
plt.contourf(np.random.random((10,10))) 
plt.colorbar() 
for k in xrange(10): 
    plt.contourf(np.random.random((10,10))) 
plt.show()