在子图之外放置多个颜色条 (matplotlib)

发布于 2025-01-14 17:17:44 字数 236 浏览 3 评论 0原文

我有一个包含多个子图行的图形,它们都共享一个 x 轴。 有些行需要颜色条,但其他行不需要。 如果我只使用颜色条功能,子图将会错位。 如何将颜色条放置在子图之外,以便所有行仍然对齐?

图与未对齐的子图

I have a figure with multiple subplot rows that all share an x axis.
Some of the rows require a color bar, but the other rows don't.
If I just use the color bar function, the subplots will be misaligned.
How do I place the color bars outside of the subplots such that all the rows will still be aligned?

figure with misaligned subplots

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2025-01-21 17:17:45

我做了一个可能有帮助的函数:

import numpy as np
from matplotlib import pyplot as plt

#function to add colorbar for imshow data and axis
def add_colorbar_outside(im,ax):
    fig = ax.get_figure()
    bbox = ax.get_position() #bbox contains the [x0 (left), y0 (bottom), x1 (right), y1 (top)] of the axis.
    width = 0.01
    eps = 0.01 #margin between plot and colorbar
    # [left most position, bottom position, width, height] of color bar.
    cax = fig.add_axes([bbox.x1 + eps, bbox.y0, width, bbox.height])
    cbar = fig.colorbar(im, cax=cax)

#Example code:
x = np.random.random((10, 100))
fig, axes = plt.subplots(5,1, sharex = True)
im = axes[0].imshow(x, cmap = "Reds", aspect="auto", origin="lower")
add_colorbar_outside(im, axes[0])
im2 = axes[2].imshow(x, cmap = "coolwarm", aspect="auto", origin="lower")
add_colorbar_outside(im2, axes[2])
plt.show()

外面有颜色条的图形

I made a function that may help:

import numpy as np
from matplotlib import pyplot as plt

#function to add colorbar for imshow data and axis
def add_colorbar_outside(im,ax):
    fig = ax.get_figure()
    bbox = ax.get_position() #bbox contains the [x0 (left), y0 (bottom), x1 (right), y1 (top)] of the axis.
    width = 0.01
    eps = 0.01 #margin between plot and colorbar
    # [left most position, bottom position, width, height] of color bar.
    cax = fig.add_axes([bbox.x1 + eps, bbox.y0, width, bbox.height])
    cbar = fig.colorbar(im, cax=cax)

#Example code:
x = np.random.random((10, 100))
fig, axes = plt.subplots(5,1, sharex = True)
im = axes[0].imshow(x, cmap = "Reds", aspect="auto", origin="lower")
add_colorbar_outside(im, axes[0])
im2 = axes[2].imshow(x, cmap = "coolwarm", aspect="auto", origin="lower")
add_colorbar_outside(im2, axes[2])
plt.show()

figure with colorbars outside

心不设防 2025-01-21 17:17:45

sharex=Truelayout='constrained' 参数(如 @drmuelr 指出的)应该足以解决您的问题。
fig,axes = plt.subplots(3,1, sharex = True,layout='constrained') 在您的情况下。

Matplotlib 现在有关于如何排列颜色条的良好文档: https:// matplotlib.org/stable/users/explain/axes/colorbar_placement.html

The sharex=True and layout='constrained' argument (as pointed out by @drmuelr) should be sufficient to solve your problem.
i.e. fig, axes = plt.subplots(3,1, sharex = True, layout='constrained') in your case.

Matplotlib by now has good documentation on how to arrange the colorbar here: https://matplotlib.org/stable/users/explain/axes/colorbar_placement.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文