在Python中调整matplotlib中各个子图的高度
如果我有一系列一列多行的子图,即:
plt.subplot(4, 1, 1) # first subplot
plt.subplot(4, 1, 2) # second subplot
# ...
如何调整前 N 个子图的高度?例如,如果我有 4 个子图,每个子图都在自己的行上,我希望它们都具有相同的宽度,但前 3 个子图更短,即它们的 y 轴更小并且占用的图比行中最后一个图的 y 轴。我该怎么做?
谢谢。
if I have a series of subplots with one column and many rows, i.e.:
plt.subplot(4, 1, 1) # first subplot
plt.subplot(4, 1, 2) # second subplot
# ...
how can I adjust the height of the first N subplots? For example, if I have 4 subplots, each on its own row, I want all of them to have the same width but the first 3 subplots to be shorter, i.e. have their y-axes be smaller and take up less of the plot than the y-axis of the last plot in the row. How can I do this?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管这个问题很老了,但我一直在寻找回答一个非常相似的问题。 @Joe 对 AxesGrid 的引用,是我问题的答案,并且非常< /strong> 用法简单,所以我想说明该功能的完整性。
AxesGrid 功能提供了创建不同大小的绘图并通过
subplot2grid
功能:请注意
row_n
,的最大值col_n
分别是m-1
和n-1
,因为使用零索引表示法。具体解决这个问题,如果总共有 5 个子图,其中最后一个子图的高度是其他子图的两倍,我们可以使用
m=6
。Even though this question is old, I was looking to answer a very similar question. @Joe's reference to AxesGrid, was the answer to my question, and has very straightforward usage, so I wanted to illustrate that functionality for completeness.
AxesGrid functionality provides the ability create plots of different size and place them very specifically, via the
subplot2grid
functionality:Note that the max values for
row_n
,col_n
arem-1
andn-1
respectively, as zero indexing notation is used.Specifically addressing the question, if there were 5 total subplots, where the last subplot has twice the height as the others, we could use
m=6
.有多种方法可以做到这一点。最基本(也是最不灵活)的方法是只调用类似的东西:
这会给你这样的东西:
但是,这不是很灵活。如果您使用的是 matplotlib >= 1.0.0,请考虑使用 GridSpec。它非常好,并且是布置子图的更灵活的方式。
There are multiple ways to do this. The most basic (and least flexible) way is to just call something like:
Which will give you something like this:
However, this isn't very flexible. If you're using matplotlib >= 1.0.0, look into using GridSpec. It's quite nice, and is a much more flexible way of laying out subplots.