python 中不均匀的子图
Uneven subplot in python
在 python 中创建 (3,3) 子图矩阵的最佳方法是什么,但要注意以下问题:
- 第一列包含 3 个子图
- 第二列包含 3 个子图
- 第三列包含 2 个子图
最后两个子图的大小应该相等。这意味着它们将在其他两列的中间图的中间相遇。
我尝试用 gridspec 来做这件事,但到目前为止还没有成功。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(3, 3)
ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)
ax3 = plt.subplot(gs[3])
ax3.plot(y, x)
ax4 = plt.subplot(gs[4])
ax4.plot(y, x)
ax6 = plt.subplot(gs[6])
ax6.plot(y, x)
ax7 = plt.subplot(gs[7])
ax7.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.png')
plt.show()
我假设你的问题出在最后一行的两个图上。一种想法是将网格视为 (3,6) 网格。前两行中的每个图使用两列,最后一行中的两个图使用 3 列。
ax1 = plt.subplot2grid((3, 6), (0, 0), colspan=2)
ax2 = plt.subplot2grid((3, 6), (0, 2), colspan=2)
ax3 = plt.subplot2grid((3, 6), (0, 4), colspan=2)
ax4 = plt.subplot2grid((3, 6), (1, 0), colspan=2)
ax5 = plt.subplot2grid((3, 6), (1, 2), colspan=2)
ax6 = plt.subplot2grid((3, 6), (1, 4), colspan=2)
ax7 = plt.subplot2grid((3, 6), (2, 0), colspan=3)
ax8 = plt.subplot2grid((3, 6), (2, 3), colspan=3)
这里不需要使用gridspec。只需将子图添加到您想要的地方即可。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
ax1= fig.add_subplot(3,3,1)
ax2= fig.add_subplot(3,3,2)
ax3= fig.add_subplot(3,3,4)
ax4= fig.add_subplot(3,3,5)
ax5= fig.add_subplot(3,3,7)
ax6= fig.add_subplot(3,3,8)
ax7= fig.add_subplot(2,3,3)
ax8= fig.add_subplot(2,3,6)
plt.show()
提供的答案作为问题的答案。以下是OP写的:
感谢@datasailor 提供的解决方案。这是一个工作示例:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(6, 3)
ax0 = plt.subplot(gs[0:2,0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[2:4,0])
ax1.plot(y, x)
ax2 = plt.subplot(gs[4:6,0])
ax2.plot(y, x)
ax3 = plt.subplot(gs[0:2,1])
ax3.plot(x, y)
ax4 = plt.subplot(gs[2:4,1])
ax4.plot(y, x)
ax5 = plt.subplot(gs[4:6,1])
ax5.plot(y, x)
ax6 = plt.subplot(gs[0:3,2])
ax6.plot(x, y)
ax7 = plt.subplot(gs[3:6,2])
ax7.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.png')
plt.show()
在 python 中创建 (3,3) 子图矩阵的最佳方法是什么,但要注意以下问题:
- 第一列包含 3 个子图
- 第二列包含 3 个子图
- 第三列包含 2 个子图
最后两个子图的大小应该相等。这意味着它们将在其他两列的中间图的中间相遇。
我尝试用 gridspec 来做这件事,但到目前为止还没有成功。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(3, 3)
ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)
ax3 = plt.subplot(gs[3])
ax3.plot(y, x)
ax4 = plt.subplot(gs[4])
ax4.plot(y, x)
ax6 = plt.subplot(gs[6])
ax6.plot(y, x)
ax7 = plt.subplot(gs[7])
ax7.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.png')
plt.show()
我假设你的问题出在最后一行的两个图上。一种想法是将网格视为 (3,6) 网格。前两行中的每个图使用两列,最后一行中的两个图使用 3 列。
ax1 = plt.subplot2grid((3, 6), (0, 0), colspan=2)
ax2 = plt.subplot2grid((3, 6), (0, 2), colspan=2)
ax3 = plt.subplot2grid((3, 6), (0, 4), colspan=2)
ax4 = plt.subplot2grid((3, 6), (1, 0), colspan=2)
ax5 = plt.subplot2grid((3, 6), (1, 2), colspan=2)
ax6 = plt.subplot2grid((3, 6), (1, 4), colspan=2)
ax7 = plt.subplot2grid((3, 6), (2, 0), colspan=3)
ax8 = plt.subplot2grid((3, 6), (2, 3), colspan=3)
这里不需要使用gridspec。只需将子图添加到您想要的地方即可。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
ax1= fig.add_subplot(3,3,1)
ax2= fig.add_subplot(3,3,2)
ax3= fig.add_subplot(3,3,4)
ax4= fig.add_subplot(3,3,5)
ax5= fig.add_subplot(3,3,7)
ax6= fig.add_subplot(3,3,8)
ax7= fig.add_subplot(2,3,3)
ax8= fig.add_subplot(2,3,6)
plt.show()
提供的答案作为问题的答案。以下是OP写的:
感谢@datasailor 提供的解决方案。这是一个工作示例:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(6, 3)
ax0 = plt.subplot(gs[0:2,0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[2:4,0])
ax1.plot(y, x)
ax2 = plt.subplot(gs[4:6,0])
ax2.plot(y, x)
ax3 = plt.subplot(gs[0:2,1])
ax3.plot(x, y)
ax4 = plt.subplot(gs[2:4,1])
ax4.plot(y, x)
ax5 = plt.subplot(gs[4:6,1])
ax5.plot(y, x)
ax6 = plt.subplot(gs[0:3,2])
ax6.plot(x, y)
ax7 = plt.subplot(gs[3:6,2])
ax7.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.png')
plt.show()