代码之家  ›  专栏  ›  技术社区  ›  Boris Gorelik

在matplotlib中添加颜色栏时出现attributeError

  •  30
  • Boris Gorelik  · 技术社区  · 15 年前

    以下代码无法在python 2.5.4上运行:

    from matplotlib import pylab as pl
    import numpy as np
    
    data = np.random.rand(6,6)
    fig = pl.figure(1)
    fig.clf()
    ax = fig.add_subplot(1,1,1)
    ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
    pl.colorbar()
    
    pl.show()
    

    错误消息是

    C:\temp>python z.py
    Traceback (most recent call last):
      File "z.py", line 10, in <module>
        pl.colorbar()
      File "C:\Python25\lib\site-packages\matplotlib\pyplot.py", line 1369, in colorbar
        ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
      File "C:\Python25\lib\site-packages\matplotlib\figure.py", line 1046, in colorbar
        cb = cbar.Colorbar(cax, mappable, **kw)
      File "C:\Python25\lib\site-packages\matplotlib\colorbar.py", line 622, in __init__
        mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
    AttributeError: 'NoneType' object has no attribute 'autoscale_None'
    

    如何将颜色栏添加到此代码中?

    以下是口译员信息:

    Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    4 回复  |  直到 6 年前
        1
  •  21
  •   Michele dfa    6 年前

    注意:我使用的是python 2.6.2。您的代码也出现了同样的错误,下面的修改解决了这个问题。

    我阅读了以下颜色条示例: http://matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html

    from matplotlib import pylab as pl
    import numpy as np
    
    data = np.random.rand(6,6)
    fig = pl.figure(1)
    fig.clf()
    ax = fig.add_subplot(1,1,1)
    img = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
    fig.colorbar(img)
    
    pl.show()
    

    不知道为什么你的例子不起作用。我对Matplotlib不太熟悉。

        2
  •  66
  •   pelson    11 年前

    (我知道这是一个非常古老的问题)您之所以看到这个问题是因为您将状态机(matplotlib.pyplot)的使用与向轴添加图像的OO方法混合在一起。

    这个 plt.imshow 函数与 ax.imshow 方法只有一个细微的不同。 方法 Ax.IimSee :

    • 创建并返回已添加到轴的图像

    函数 皮特-伊姆塞 :

    • 创建并返回已添加到当前轴的图像,并将该图像设置为“当前”图像/可映射(然后可以由 plt.colorbar 函数)。

    如果你想使用 彩条 (除了最极端的情况外,你都是这样做的)与 Ax.IimSee 方法,您将需要传递返回的图像(它是 ScalarMappable 彩条 作为第一个论点:

    plt.imshow(image_file)
    plt.colorbar()
    

    相当于(不使用状态机)到:

    img = ax.imshow(image_file)
    plt.colorbar(img, ax=ax)
    

    如果ax是Pyplot中的当前轴,那么kwarg ax=ax 不需要。

        3
  •  1
  •   roob    6 年前

    我在一个教程中找到了这个问题的另一个解决方案。

    下面的代码对于plt.imshow()方法很有用:

    def colorbar(Mappable, Orientation='vertical', Extend='both'):
        Ax = Mappable.axes
        fig = Ax.figure
        divider = make_axes_locatable(Ax)
        Cax = divider.append_axes("right", size="5%", pad=0.05)
        return fig.colorbar(
            mappable=Mappable, 
            cax=Cax,
            use_gridspec=True, 
            extend=Extend,  # mostra um colorbar full resolution de z
            orientation=Orientation
        )
    
    fig, ax = plt.subplots(ncols=2)
    
    img1 = ax[0].imshow(data)
    colorbar(img1)
    
    img2 = ax[1].imshow(-data)
    colorbar(img2)
    
    fig.tight_layout(h_pad=1)
    plt.show()
    

    它可能不适合其他绘图方法。例如,它不适用于geopandas地理数据框架图。

        4
  •  -1
  •   Ishan Tomar    8 年前

    在代码中添加/编辑以下行

    plot = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
    pl.colorbar(plot)