使用WindroseAxes.bar时遇matplotlib无colormaps属性错误求解决
问题解决:AttributeError: module 'matplotlib' has no attribute 'colormaps'
错误原因
matplotlib.colormaps是Matplotlib 3.10及以上版本才引入的新API,你的Matplotlib版本低于该版本,因此无法识别这个属性。
解决方法
有两种可行方案:
方案1:升级Matplotlib到最新稳定版
执行命令更新:pip install --upgrade matplotlib升级后即可正常使用
mpl.colormaps['magma']的写法。方案2:兼容旧版本的写法(无需升级)
改用Matplotlib全版本支持的get_cmap方法,或者直接引用cm模块下的颜色映射对象:# 方法A:使用get_cmap获取颜色映射 import matplotlib as mpl cmap = mpl.cm.get_cmap('magma') ax.bar(wd, ws, normed=True, opening=1, edgecolor='white', bins=speed_bin, cmap=cmap, nsector=12) # 方法B:直接调用cm模块下的预定义颜色映射 import matplotlib as mpl cmap = mpl.cm.magma ax.bar(wd, ws, normed=True, opening=1, edgecolor='white', bins=speed_bin, cmap=cmap, nsector=12)
补充说明
两种兼容写法在新旧Matplotlib版本中都能正常运行,适合无法升级依赖的场景。
内容的提问来源于stack exchange,提问作者Justin
相关产品推荐
相关产品推荐

