python - 如何使用 ImageGrid 向颜色栏添加标签?

标签 python python-2.7 matplotlib colorbar

在上一个问题中, colobar label matplotlib in ImageGrid , 有一个向颜色条添加标签的解决方案,但这似乎与当前版本不兼容。

我尝试过的平台:

  • 带顶篷的 Mac:
    • python :2.7
    • matplotlib: 1.4.3-6
  • Linux:
    • python :2.7
    • matplotlib: 1.3.1

下面是上一个问题的代码,还有一些在 iPython notebook 中运行的额外代码:

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'

自从提出原始问题后,matplotlib 接口(interface)是否发生了变化?如果是这样,我该如何添加颜色条标签?

最佳答案

就个人而言,我一直认为 matplotlib 是黑魔法,类似于 TeX,所以我不能保证我的回答是“官方”做你的事情想要,或者它将继续在以后的版本中工作。但感谢this gallery example ,我可以想出以下咒语:

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)。结果:

enter image description here

关于python - 如何使用 ImageGrid 向颜色栏添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469579/

相关文章:

python - Pandas 字符串按可变长度位置过滤

python - 取消引用 hdf5 格式?

python - 如何使用元类创建一个类属性,该元类仅在类本身未定义时才覆盖?

python - 在python中搜索特定字符串

python - 选择图像区域(以编程方式!): Python 3, matplotlib

Python:raw_input 读取数字的问题

python - 如何使用 Node 或 ClojureScript 在命令行上的当前文件夹中启动 Web 服务器?

python - 导入错误 : Could not import the Python Imaging Library (PIL) required to load image files on tensorflow

matplotlib - imshow 非均匀矩阵 bin 大小

python - 移植涉及 matplotlib 设置轴位置(以像素为单位)的 Matlab 代码