1:2:1 的 matplotlib 子图
matplotlib subplot of 1:2:1
我正在尝试绘制具有以下布局的 A4 PDF:
- 1 个图表跨越 2 列
- 2 个图表,每个跨越 1 列
- 1 个图表跨越 2 列
我有以下代码:
fig = plt.figure(figsize=(8.27,11.69))
ax = fig.add_subplot(311)
ax = fig.add_subplot(323)
ax = fig.add_subplot(324)
ax = fig.add_subplot(315)
但我收到以下错误:
ValueError: num must be 1 <= num <= 3, not 5
我错过了什么?
这是正确的语法:
fig = plt.figure()
ax = fig.add_subplot(311)
ax = fig.add_subplot(323)
ax = fig.add_subplot(324)
ax = fig.add_subplot(313)
但是,对于这种事情,如果您使用 GridSpec
https://matplotlib.org/3.1.1/tutorials/intermediate/gridspec.html,您将获得可读性。下面的代码产生完全相同的输出,但(至少对我而言)更容易明白
fig = plt.figure()
gs = fig.add_gridspec(3, 2)
fig.add_subplot(gs[0, :])
fig.add_subplot(gs[1, 0])
fig.add_subplot(gs[1, 1])
fig.add_subplot(gs[2, :])
我正在尝试绘制具有以下布局的 A4 PDF:
- 1 个图表跨越 2 列
- 2 个图表,每个跨越 1 列
- 1 个图表跨越 2 列
我有以下代码:
fig = plt.figure(figsize=(8.27,11.69))
ax = fig.add_subplot(311)
ax = fig.add_subplot(323)
ax = fig.add_subplot(324)
ax = fig.add_subplot(315)
但我收到以下错误:
ValueError: num must be 1 <= num <= 3, not 5
我错过了什么?
这是正确的语法:
fig = plt.figure()
ax = fig.add_subplot(311)
ax = fig.add_subplot(323)
ax = fig.add_subplot(324)
ax = fig.add_subplot(313)
但是,对于这种事情,如果您使用 GridSpec
https://matplotlib.org/3.1.1/tutorials/intermediate/gridspec.html,您将获得可读性。下面的代码产生完全相同的输出,但(至少对我而言)更容易明白
fig = plt.figure()
gs = fig.add_gridspec(3, 2)
fig.add_subplot(gs[0, :])
fig.add_subplot(gs[1, 0])
fig.add_subplot(gs[1, 1])
fig.add_subplot(gs[2, :])