如何使用ImageGrid将标签添加到颜色栏? [英] How can I add a label to colorbar using ImageGrid?

查看:39
本文介绍了如何使用ImageGrid将标签添加到颜色栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中,

In a previous question, colobar label matplotlib in ImageGrid, had a solution for adding a label to the colorbar, but this seems to be broken with the current version.

Platforms I have tried:

  • Mac w/ Canopy:
    • python: 2.7
    • matplotlib: 1.4.3-6
  • Linux:
    • python: 2.7
    • matplotlib: 1.3.1

Below is the code from the previous question, with some extra code for running in an 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()

The error message from the code is of the form:

---------------------------------------------------------------------------
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'

Has the matplotlib interface changed since the original question was asked? If so, how do I add the colorbar label?

解决方案

Personally, I've always perceived matplotlib as black magic, similar to TeX, so I cannot guarantee that my answer is the "official" way of doing what you want, or that it will continue to work in later versions. But thanks to this gallery example, I could devise the following incantation:

grid[0].cax.colorbar(im)
cax = grid.cbar_axes[0]
axis = cax.axis[cax.orientation]
axis.label.set_text("$[a.u.]$")

(don't forget to remove all your colorbar-related code). This works in the current matplotlib version (1.4.3). The result:

这篇关于如何使用ImageGrid将标签添加到颜色栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆