Python 如何使用ImageGrid将标签添加到colorbar?

Python 如何使用ImageGrid将标签添加到colorbar?,python,python-2.7,matplotlib,colorbar,Python,Python 2.7,Matplotlib,Colorbar,在前一个问题中, , 我们有一个向颜色栏添加标签的解决方案,但在当前版本中,这似乎被打破了 我尝试过的平台: Mac带雨棚: python:2.7 matplotlib:1.4.3-6 Linux: python:2.7 matplotlib:1.3.1 以下是上一个问题的代码,以及在iPython笔记本中运行的一些额外代码: import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid d

在前一个问题中, , 我们有一个向颜色栏添加标签的解决方案,但在当前版本中,这似乎被打破了

我尝试过的平台:

  • Mac带雨棚:
    • python:2.7
    • matplotlib:1.4.3-6
  • Linux:
    • python:2.7
    • matplotlib:1.3.1
以下是上一个问题的代码,以及在iPython笔记本中运行的一些额外代码:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid

def get_demo_image():
    import numpy as np
    from matplotlib.cbook import get_sample_data
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
    z = np.load(f)
    # z is a numpy array of 15x15
    return z, (-3,4,-4,3)

def demo_grid_with_single_cbar(fig):
    """
    A grid of 2x2 images with a single colorbar
    """
    grid = AxesGrid(fig, 132, # similar to subplot(132)
                    nrows_ncols = (2, 2),
                    axes_pad = 0.0,
                    share_all=True,
                    label_mode = "L",
                    cbar_location = "top",
                    cbar_mode="single",
                    )

    Z, extent = get_demo_image()
    for i in range(4):
        im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
    #plt.colorbar(im, cax = grid.cbar_axes[0])
    #grid.cbar_axes[0].colorbar(im)
    cbar = grid.cbar_axes[0].colorbar(im)
    cbar.ax.set_label_text("$[a.u.]$")

    for cax in grid.cbar_axes:
        cax.toggle_label(False)

    # This affects all axes as share_all = True.
    grid.axes_llc.set_xticks([-2, 0, 2])
    grid.axes_llc.set_yticks([-2, 0, 2])
#
F = plt.figure(1, (10.5, 2.5))
F.subplots_adjust(left=0.05, right=0.95)
demo_grid_with_single_cbar(F)

plt.draw()
plt.show()
代码中的错误消息的格式如下:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-60ebdb832699> in <module>()
     40 F = plt.figure(1, (10.5, 2.5))
     41 F.subplots_adjust(left=0.05, right=0.95)
---> 42 demo_grid_with_single_cbar(F)
     43 
     44 plt.draw()

<ipython-input-1-60ebdb832699> in demo_grid_with_single_cbar(fig)
     29     #grid.cbar_axes[0].colorbar(im)
     30     cbar = grid.cbar_axes[0].colorbar(im)
---> 31     cbar.ax.set_label_text("$[a.u.]$")
     32 
     33     for cax in grid.cbar_axes:

AttributeError: 'CbarAxes' object has no attribute 'set_label_text'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
40 F=plt.图(1,(10.5,2.5))
41 F.子批次调整(左=0.05,右=0.95)
--->42带单个cbar的演示网格(F)
43
44 plt.draw()
在演示网格中,使用单个cbar(图)
29#grid.cbar_轴[0]。颜色条(im)
30 cbar=栅格。cbar_轴[0]。颜色条(im)
--->31 cbar.ax.set_标签_文本($[a.u.]$)
32
33对于网格中的cax.cbar_轴:
AttributeError:“CbarAxes”对象没有属性“set\u label\u text”

matplotlib接口自原始问题提出后是否已更改?如果是这样,我该如何添加色条标签?

就我个人而言,我一直认为
matplotlib
是一种黑魔法,类似于
TeX
,因此我不能保证我的答案是你想要的“官方”方式,或者它将在以后的版本中继续工作。但多亏了他的帮助,我可以设计出以下咒语:

grid[0].cax.colorbar(im)
cax = grid.cbar_axes[0]
axis = cax.axis[cax.orientation]
axis.label.set_text("$[a.u.]$")
(不要忘记删除所有与色条相关的代码)。这适用于当前的
matplotlib
版本(1.4.3)。结果是:

谢谢!我有时也会有同样的“黑魔法”感觉,但我用得越多,我就越能理解!