定位颜色条

82
我有一个带有颜色条的matplotlib图。我想把颜色条放在图的下方,并使其水平显示。
我已经通过以下方式几乎完成了这个要求:
plt.colorbar(orientation="horizontal",fraction=0.07,anchor=(1.0,0.0))

但是颜色条仍然与图表稍微重叠(以及x轴的标签)。我想把颜色条向下移动,但是我不知道如何做到。
3个回答

121

使用填充 pad

为了相对于子图移动色条,可以使用fig.colorbarpad参数。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")

fig.colorbar(im, orientation="horizontal", pad=0.2)
plt.show()

这里输入图片描述

使用坐标轴分隔器

可以使用make_axes_locatable的实例来划分轴并创建一个新的轴,该轴与图像绘制完全对齐。同样,pad参数可以设置两个轴之间的间距。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np; np.random.seed(1)

fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")

divider = make_axes_locatable(ax)
cax = divider.new_vertical(size="5%", pad=0.7, pack_start=True)
fig.add_axes(cax)
fig.colorbar(im, cax=cax, orientation="horizontal")

plt.show()

在此输入图片描述

使用子图

可以直接创建两行子图,一行用于图片显示,一行用于颜色条的显示。然后,在创建图形时将 height_ratios 设置为 gridspec_kw={"height_ratios":[1, 0.05]} ,使得一个子图比另一个子图的高度小很多,这个较小的子图可以用来放置颜色条。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

fig, (ax, cax) = plt.subplots(nrows=2,figsize=(4,4), 
                  gridspec_kw={"height_ratios":[1, 0.05]})
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")

fig.colorbar(im, cax=cax, orientation="horizontal")

plt.show()

输入图像描述


3
我该如何使用你的第二个示例将色条放置在图像上方?我尝试设置cax = divider.append_axes("top", size="5%", pad=.15),但刻度线会重叠在图像上。我一直在尝试按照这个答案向外放置它,但是没有成功,因为该答案使用了非常与众不同的matplotlib对象,而我无法在你的示例中复现那种方法。 - fabda01
@T.Boutelier 是的,这是可以预料的。shrink 不适用于外部创建的轴。 - ImportanceOfBeingErnest
通过创建一个2x3的网格(而不是2x1),让图像填充所有3列,但色条只在中间一列。 - ImportanceOfBeingErnest
1
正确。您可以使用GridSpec。 - ImportanceOfBeingErnest
@ImportanceOfBeingErnest,如果要在跨越多个图的情况下应用gridspec来创建颜色条,你会怎么做? - Charles
显示剩余4条评论

81

编辑:已更新至 matplotlib 版本 >= 3。

已经在此回答中分享了三种很好的做法。

matplotlib 文档建议使用 inset_locator 来实现。具体如下:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np

rng = np.random.default_rng(1)

fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(rng.random((11, 16)))
ax.set_xlabel("x label")

axins = inset_axes(ax,
                    width="100%",  
                    height="5%",
                    loc='lower center',
                    borderpad=-5
                   )
fig.colorbar(im, cax=axins, orientation="horizontal")

代码输出


23
请提供一个可以运行的完整示例。 - user2545464
4
我认为你想使用img = ax1.imshow(your_data)代替ax1.plot(your_data),然后最后一行是cb = plt.colorbar(img, cax=cbaxes) - Terry Brown
我喜欢这个答案,因为它在我的示例中非常好用。除了当我添加颜色条的标题时。那时我就看不到了。你能帮忙吗? - Maikefer

1
这在matplotlib 3.7版本中变得更加容易,现在你可以简单地使用location='bottom'关键字。
docs中可以看到:
location : None or {'left', 'right', 'top', 'bottom'}
    The location, relative to the parent axes, where the colorbar axes
    is created.  It also determines the *orientation* of the colorbar
    (colorbars on the left and right are vertical, colorbars at the top
    and bottom are horizontal).  If None, the location will come from the
    *orientation* if it is set (vertical colorbars on the right, horizontal
    ones at the bottom), or default to 'right' if *orientation* is unset.

例子:

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(1)

fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(rng.random((11, 16)))
ax.set_xlabel("x label")
fig.colorbar(im, location='bottom')

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接