你好我在Python中有这个代码:
import numpy as np
import pylab as plt
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*2*np.pi*t)
fig, (ax, ax1, ax2, ax3, ax4, ax5, ax6, ax7) = plt.subplots(8,1)
ax.plot(t, s, 'o', color = "red")
ax1.plot(t, s, 'o', color = "red")
ax2.plot(t, s, 'o', color = "red")
ax3.plot(t, s, 'o', color = "red")
ax4.plot(t, s, 'o', color = "red")
ax5.plot(t, s, 'o', color = "red")
ax6.plot(t, s, 'o', color = "red")
ax7.plot(t, s, 'o', color = "red")
plt.axis([0, 1, -1, 1])
plt.show()
一切正常,但我只想制作4x2而非8x1表格的情节我尝试用plt.subplots(8,1)
替换plt.subplots(4,2)
,但我得ValueError: need more than 4 values to unpack
如何解决此问题?
答案 0 :(得分:2)
当你执行plt.subplots(4,2)
时,你不会得到一个平坦的轴列表。如果你这样做:
fig, axes = plt.subplots(4,2)
print(axes)
您将获得以下内容:
[[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000050CCBA8>
<matplotlib.axes._subplots.AxesSubplot object at 0x00000000059A0F60>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005A24A58>
<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005A896A0>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005AC37B8>
<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005B4EFD0>]
[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005B5FF60>
<matplotlib.axes._subplots.AxesSubplot object at 0x0000000005C18D30>]]
即。列表,其中每个元素对应一行子图。因此,如果你这样做:
import numpy as np
import pylab as plt
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*2*np.pi*t)
fig, axes = plt.subplots(4,2)
axes[0][0].plot(t, s, 'o', color = "red")
axes[0][1].plot(t, s, 'o', color = "red")
axes[1][0].plot(t, s, 'o', color = "red")
axes[1][1].plot(t, s, 'o', color = "red")
axes[2][0].plot(t, s, 'o', color = "red")
axes[2][1].plot(t, s, 'o', color = "red")
axes[3][0].plot(t, s, 'o', color = "red")
axes[3][1].plot(t, s, 'o', color = "red")
plt.axis([0, 1, -1, 1])
plt.show()