在 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()
这里不需要使用 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)