我正在尝试在(5)个图表上绘制(5)个熊猫系列。我想让它们看起来像这样,最后一行第五个图表本身居中(不要只关注图表的内容,而只关注位置)
但是在熊猫0.16.1中,我在第五张图表上出现了索引错误。这是我的问题的MCVE示例,你们可以复制并粘贴并自己尝试。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
fig = plt.figure()
firstperiod = pd.Series({1:1, 2:2, 3:3, 4:3, 5:4})
secondperiod = pd.Series({1:1, 2:1, 3:2, 4:2})
thirdperiod = pd.Series({1:4, 2:4, 3:5, 4:1, 5:1, 6:3})
fourthperiod = pd.Series({1:3, 2:3, 3:1, 4:6, 5:7, 6:5})
fifthperiod = pd.Series({1:2, 2:2, 3:1, 4:5, 5:5})
ax1 = plt.subplot2grid((6,4), [0,0], 2, 2)
firstperiod.plot(kind='hist', bins=5)
ax2 = plt.subplot2grid((6,4), [0,2], 2, 2)
secondperiod.plot(kind='hist', bins=4)
ax3 = plt.subplot2grid((6,4), [2,1], 2, 2)
thirdperiod.plot(kind="hist", bins=6)
ax4 = plt.subplot2grid((6,4), [2,2], 2, 2)
fourthperiod.plot(kind="hist", bins=6)
ax5 = plt.subplot2grid((6,4), [4,1], 2, 2)
fifthperiod.plot(kind="hist", bins=5)
plt.tight_layout()
plt.show()
它执行到最后一个(第5个)图表,然后弹出:
IndexError: index 4 is out of bounds for axis 0 with size 4
是什么赋予了?以及如何解决这个问题?
在此先感谢大家,谢谢。
这就是您所追求的...我添加plt.ion()
了将其置于交互模式,并plt.draw()
在定义它时添加了每个图形。
通过这样做,很明显,该轴被定义(y,x)
为非(x,y)
...
该参考资料将来也可能会有所帮助
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
plt.ion()
fig = plt.figure()
firstperiod = pd.Series({1:1, 2:2, 3:3, 4:3, 5:4})
secondperiod = pd.Series({1:1, 2:1, 3:2, 4:2})
thirdperiod = pd.Series({1:4, 2:4, 3:5, 4:1, 5:1, 6:3})
fourthperiod = pd.Series({1:3, 2:3, 3:1, 4:6, 5:7, 6:5})
fifthperiod = pd.Series({1:2, 2:2, 3:1, 4:5, 5:5})
ax1 = plt.subplot2grid((3,4), (0,0), colspan=2)
firstperiod.plot(kind='hist', bins=5)
plt.draw()
raw_input()
ax2 = plt.subplot2grid((3,4), (0,2), colspan=2)
secondperiod.plot(kind='hist', bins=4)
plt.draw()
raw_input()
ax3 = plt.subplot2grid((3,4), (1,0), colspan=2)
thirdperiod.plot(kind="hist", bins=6)
plt.draw()
raw_input()
ax4 = plt.subplot2grid((3,4), (1,2), colspan=2)
fourthperiod.plot(kind="hist", bins=6)
plt.draw()
raw_input()
ax5 = plt.subplot2grid((3,4), (2,1), colspan=2)
fifthperiod.plot(kind="hist", bins=5)
plt.draw()
raw_input()
plt.tight_layout()
plt.draw()
我将列高从6更改为3,因为您在上方显示的图形的colspan
两倍是rowspan
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句