You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 00:05:02