【问题标题】:AttributeError while adding colorbar in matplotlib在 matplotlib 中添加颜色条时出现 AttributeError
【发布时间】:2020-05-31 09:36:44
【问题描述】:

以下代码无法在 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.
>>>

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

    plt.imshow 函数与ax.imshow 方法的不同之处仅在于一种细微的不同。 方法ax.imshow:

    • 创建并返回已添加到坐标区的图像

    函数plt.imshow:

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

    如果您希望能够通过 ax.imshow 方法使用 plt.colorbar(除了最极端的情况外,您都可以使用),您将需要传递返回的图像(它是ScalarMappable) 到 plt.colorbar 作为第一个参数:

    plt.imshow(image_file)
    plt.colorbar()
    

    等价于(不使用状态机):

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

    如果 ax 是 pyplot 中的当前坐标区,则不需要 kwarg ax=ax。

    【讨论】:

    • 有没有办法从轴对象本身访问图像?假设您正在编写一个函数,该函数传递一个轴对象,该对象已经绘制了一个图像,并且您希望您的函数添加一个颜色条。我想做类似 plt.colorbar(ax.image,ax=ax) 的事情。
    • 您可以使用坐标区上的images 属性来获取坐标区中的图像列表。或者,坐标轴上的“活动”图像可通过gci 获得(来自内存)。
    • 将plt.colorbar() 更改为fig.colorbar() 是否同样有效?
    【解决方案2】:

    注意:我使用的是 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 不是很熟悉。

    【讨论】:

      【解决方案3】:

      我在教程中找到了解决此问题的另一种方法。

      以下代码适用于 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 Geodataframe plot。

      【讨论】:

      • 由于您提到了外部资源(教程),因此包含指向该资源的链接会很有用。
      • ax.figure 是一个令人惊叹的了解工具。它允许很多功能,而无需将 fig 直接传递给函数。
      【解决方案4】:

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

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

      【讨论】:

      • 没有回答问题
      • @invictus 答案与 Gary 给出的类似。如果您努力尝试并使用我编写的任何内容编辑代码,它的答案会令人满意。提问者的代码不起作用,因为 pl.colorbar() 给出了错误的实例来绘制。代码中定义的'ax'默认是一个窗口实例。 pl.colorbar() 必须指向绘图实例。在我的例子中,'plot' 是 pl.colorbar() 所需要的,它类似于 Gary 给出的 'cax'
      猜你喜欢
      • 2014-01-12
      • 2016-06-07
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多