Python中的matplotlib中的子图中心不均匀?

发布于 2024-09-05 21:39:00 字数 892 浏览 1 评论 0原文

我正在 matplotlib 中绘制一对简单的子图,由于某种原因,它们的中心不均匀。我将它们绘制如下:

plt.figure()
# first subplot
s1 = plt.subplot(2, 1, 1)
plt.bar([1, 2, 3], [4, 5, 6])
# second subplot
s2 = plt.subplot(2, 1, 2)
plt.pcolor(rand(5,5))

# add colorbar
plt.colorbar()

# square axes
axes_square(s1)
axes_square(s2)

其中axes_square很简单:

def axes_square(plot_handle):
  plot_handle.axes.set_aspect(1/plot_handle.axes.get_data_ratio()) 

我得到的图已附上。顶部和底部图的中心不均匀。我希望它们的 y 轴对齐并且它们的框对齐。

如果我删除 plt.colorbar() 调用,绘图就会居中。如何在仍显示 pcolor 的颜色条的情况下使绘图居中?我希望轴居中,并且颜色条位于该对齐方式之外,位于 pcolor 矩阵的左侧或右侧。

绘图链接图像

matplotlib 中未居中子图的图像

谢谢。

I am plotting a simple pair of subplots in matplotlib that are for some reason unevenly centered. I plot them as follows:

plt.figure()
# first subplot
s1 = plt.subplot(2, 1, 1)
plt.bar([1, 2, 3], [4, 5, 6])
# second subplot
s2 = plt.subplot(2, 1, 2)
plt.pcolor(rand(5,5))

# add colorbar
plt.colorbar()

# square axes
axes_square(s1)
axes_square(s2)

where axes_square is simply:

def axes_square(plot_handle):
  plot_handle.axes.set_aspect(1/plot_handle.axes.get_data_ratio()) 

The plot I get is attached. The top and bottom plots are unevenly centered. I'd like their yaxis to be aligned and their boxes to be aligned.

If I remove the plt.colorbar() call, the plots become centered. How can I have the plots centered while the colorbar of pcolor is still shown? I want the axes to be centered and have the colorbar be outside of that alignment, either to the left or to the right of the pcolor matrix.

image of plots link

Image of uncentered subplots in matplotlib

thanks.

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

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

发布评论

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

评论(2

懵少女 2024-09-12 21:39:00

好吧,这可能不完全是您想要的,但它会起作用:

from numpy.random import rand
import matplotlib.pyplot as plt

plt.figure()
# first subplot
s1 = plt.subplot(2, 2, 2)
plt.bar([1, 2, 3], [4, 5, 6])
# second subplot
s2 = plt.subplot(2, 2, 4)
plt.pcolor(rand(5,5))

# square axes
axes_square(s1)
axes_square(s2)

s_fake = plt.subplot(2, 2, 3)
s_fake.set_axis_off()
# add colorbar
plt.colorbar()

它只是在左侧生成一对假单元格,并且不会在其中显示任何内容。不漂亮,但它有效。 ;)

另外,我猜测这两个导入是隐含在您的代码中的......

Well, this probably isn't exactly what you want, but it'll work:

from numpy.random import rand
import matplotlib.pyplot as plt

plt.figure()
# first subplot
s1 = plt.subplot(2, 2, 2)
plt.bar([1, 2, 3], [4, 5, 6])
# second subplot
s2 = plt.subplot(2, 2, 4)
plt.pcolor(rand(5,5))

# square axes
axes_square(s1)
axes_square(s2)

s_fake = plt.subplot(2, 2, 3)
s_fake.set_axis_off()
# add colorbar
plt.colorbar()

It just makes a fake pair of cells on the left, and doesn't display anything in them. Not pretty, but it works. ;)

Also, I'm guessing that those two imports were implicit in your code ...

才能让你更想念 2024-09-12 21:39:00

为 colorbar 提供一个 cax 参数来指定您喜欢的 colorbar 位置:

plt.colorbar(cax=plt.gcf().add_axes((0.75,0.1,0.05,0.3)))

Give a cax argument to colorbar to designate where you like the colorbar:

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