如何使用pyplot/gridspec增加单个子图形的大小?

2024-04-20 00:59:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个6x4的网格中绘制23个图形,其中一个图形的宽度是其他图形的两倍。我使用的是gridspec,我当前的代码是:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

x = np.arange(0, 7, 0.01)

fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)

for n in range(22):
    ax = fig.add_subplot(gs[n])
    ax.plot(x, np.sin(0.2*n*x))

corrax = fig.add_subplot(gs[22])
fig.tight_layout()
plt.show()

这将产生以下结果:

Figure grid

我想增加最右边的绘图在最下面一行的宽度,这样它会占用该行中剩余的空间。有没有办法做到这一点?在


Tags: importgsadd图形网格宽度matplotlibas
2条回答
#corrax = fig.add_subplot(gs[5,2:])
corrax = fig.add_subplot(6,4,(23,24))

两者都应该有用。在

您可以使用切片从gridspec中选择几个位置,例如gs[22:24]。在

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

x = np.arange(0, 7, 0.01)

fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)

for n in range(22):
    ax = fig.add_subplot(gs[n])
    ax.plot(x, np.sin(0.2*n*x))

corrax = fig.add_subplot(gs[22:24])
corrax.plot(x,np.sin(0.2*22*x), color="crimson", lw=3)
fig.tight_layout()
plt.show()

enter image description here

您也可以对gridspec进行二维切片。E、 要创建一个3x3网格并使右下角的绘图跨越两列两行,可以像gs[1:,1:]那样进行切片。在

^{pr2}$

enter image description here

相关问题 更多 >