Python中的matplotlib中的子图中心不均匀?
我正在 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 矩阵的左侧或右侧。
谢谢。
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.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这可能不完全是您想要的,但它会起作用:
它只是在左侧生成一对假单元格,并且不会在其中显示任何内容。不漂亮,但它有效。
;)
另外,我猜测这两个导入是隐含在您的代码中的......
Well, this probably isn't exactly what you want, but it'll work:
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 ...
为 colorbar 提供一个
cax
参数来指定您喜欢的 colorbar 位置:Give a
cax
argument to colorbar to designate where you like the colorbar: