在Python中调整matplotlib中各个子图的高度

发布于 2024-09-11 20:14:02 字数 254 浏览 5 评论 0原文

如果我有一系列一列多行的子图,即:

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 技术交流群。

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

发布评论

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

评论(2

岛歌少女 2024-09-18 20:14:02

尽管这个问题很老了,但我一直在寻找回答一个非常相似的问题。 @Joe 对 AxesGrid 的引用,是我问题的答案,并且非常< /strong> 用法简单,所以我想说明该功能的完整性。

AxesGrid 功能提供了创建不同大小的绘图并通过 subplot2grid 功能:

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid((m, n), (row_1, col_1), colspan = width)
ax2 = plt.subplot2grid((m, n), (row_2, col_2), rowspan = height)

ax1.plot(...)
ax2.plot(...)

请注意 row_n,的最大值col_n 分别是 m-1n-1,因为使用零索引表示法。

具体解决这个问题,如果总共有 5 个子图,其中最后一个子图的高度是其他子图的两倍,我们可以使用 m=6

ax1 = plt.subplot2grid((6, 1), (0, 0))
ax2 = plt.subplot2grid((6, 1), (1, 0))
ax3 = plt.subplot2grid((6, 1), (2, 0))
ax4 = plt.subplot2grid((6, 1), (3, 0))
ax5 = plt.subplot2grid((6, 1), (4, 0), rowspan=2)
plt.show()

最后一张图,高度的两倍

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:

import matplotlib.pyplot as plt

ax1 = plt.subplot2grid((m, n), (row_1, col_1), colspan = width)
ax2 = plt.subplot2grid((m, n), (row_2, col_2), rowspan = height)

ax1.plot(...)
ax2.plot(...)

Note that the max values for row_n,col_n are m-1 and n-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.

ax1 = plt.subplot2grid((6, 1), (0, 0))
ax2 = plt.subplot2grid((6, 1), (1, 0))
ax3 = plt.subplot2grid((6, 1), (2, 0))
ax4 = plt.subplot2grid((6, 1), (3, 0))
ax5 = plt.subplot2grid((6, 1), (4, 0), rowspan=2)
plt.show()

Last Graph, twice the height

终止放荡 2024-09-18 20:14:02

有多种方法可以做到这一点。最基本(也是最不灵活)的方法是只调用类似的东西:

import matplotlib.pyplot as plt
plt.subplot(6,1,1)
plt.subplot(6,1,2)
plt.subplot(6,1,3)
plt.subplot(2,1,2)

这会给你这样的东西:
Unequal Subplots

但是,这不是很灵活。如果您使用的是 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:

import matplotlib.pyplot as plt
plt.subplot(6,1,1)
plt.subplot(6,1,2)
plt.subplot(6,1,3)
plt.subplot(2,1,2)

Which will give you something like this:
Unequal Subplots

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.

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