Matplotlib subplot2grid在熊猫中绘制IndexError 0.16.1

麻辣酱

我正在尝试在(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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用matplotlib绘制0/1数组

如何使用 matplotlib 绘制 1 和 0 的列表?

Matplotlib subplot2grid无法正常工作

矩阵大小不兼容:DCGAN中In [0]:[16,1024],In [1]:[16384,1]

Matplotlib subplot2grid在设置gridspec_kw width_ratios时删除子图

为什么<< 256 :: size(16)>>呈现为<< 1、0 >>?

matplotlib 绘制 2 行而不是 1

matplotlib的mpl_toolkits.axes_grid1中的host_subplot参数是什么?

在R中将(0,1,0,0,1,1,1)转换为(0,0,0,1,0,1,2)

Python MatplotLib绘制x轴,第一个x轴值标记为1(而不是0)

Matplotlib ValueError:num必须为1 <= num <= 20,而不是0

Matplotlib:16 个网格中的 16 个子图:列出索引超出范围

使用sklearn和线性回归的错误:形状(1,16)和(1,1)不对齐:16(dim 1)!= 1(dim 0)

快速乘法模2 ^ 16 +1

如何使用 R 创建向量 (10^16 + 1, 10^16 + 2, ... , 10^16 + 1000)?

如何使用a [0]-a [0] a [1]-a [0] a [1] a [2]输出数组

将float从0转换为1到RGB 16bit

在matplotlib中绘制熊猫日期

我希望输出像[[0,0,0,0,],[0,1,0,0],[0,2,0,0],[0,3,0,0]]

为什么在Haskell中0 ^ 0 == 1?

如何针对i = [1:16]绘制y = x + i?

Matplotlib仅在堆叠条形图上每2条中绘制1条

在MATPLOTLIB中的1张图中绘制2个数据集+线性回归

索引 = 2 不在 [0, 1)

python中输出-1或0或1的函数

bash中1>&0的行为

计划中的0/1背包

无论我发送1还是0数组,Python matplotlib imshow总是给我白色

使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]