gpt4 book ai didi

matplotlib - matlibplot : How to add space between some subplots

转载 作者:行者123 更新时间:2023-12-03 09:57:42 26 4
gpt4 key购买 nike

如何调整某些子图之间的空白?在下面的示例中,假设我想消除第 1 个和第 2 个子图之间以及第 3 个和第 4 个之间的所有空白,并增加第 2 个和第 3 个之间的空间?

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ax = plt.subplots(4,figsize=(10,10),sharex=True)

ax[0].plot(x, y)
ax[0].set_title('Panel: A')

ax[1].plot(x, y**2)

ax[2].plot(x, y**3)
ax[2].set_title('Panel: B')
ax[3].plot(x, y**4)

plt.tight_layout()

最佳答案

为了使解决方案接近您的代码,您可以使用创建 5 个子图,中间一个是其他子图的四分之一,然后删除中间图。

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ax = plt.subplots(5,figsize=(7,7),sharex=True,
gridspec_kw=dict(height_ratios=[4,4,1,4,4], hspace=0))

ax[0].plot(x, y)
ax[0].set_title('Panel: A')

ax[1].plot(x, y**2)

ax[2].remove()

ax[3].plot(x, y**3)
ax[3].set_title('Panel: B')
ax[4].plot(x, y**4)


plt.tight_layout()
plt.show()

enter image description here

关于matplotlib - matlibplot : How to add space between some subplots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49781442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com