【问题标题】:Matplotlib subplot2grid plotting IndexError in pandas 0.16.1Matplotlib subplot2grid 在 pandas 0.16.1 中绘制 IndexError
【发布时间】:2015-08-19 21:20:08
【问题描述】:

我有 (5) 个熊猫系列,我正试图在 (5) 个图表上绘制。我想让它们看起来像这种格式,最后一行的第 5 个图表本身,居中(不要关注图表的内容,只关注定位)

但在 pandas 0.16.1 中,我在第 5 个图表上出现索引错误。这是我的问题的 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

什么给了?以及如何解决这个问题?

在此先感谢大家,谢谢。

【问题讨论】:

    标签: python pandas matplotlib histogram


    【解决方案1】:

    这就是您所追求的...我添加了plt.ion() 以使其进入交互模式,并添加plt.draw() 以添加您定义的每个图形。

    通过这样做,很明显轴被定义为(y,x) 而不是(x,y)...

    This reference 将来可能也会有所帮助

    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,因为您在上面显示的图表中 colspanrowspan 的两倍

    【讨论】:

    • 这对你有用吗?这并没有为我绘制任何东西-plt.tight_layout 行给了我这样的错误:ValueError: max() arg is an empty sequence
    • 我让你的版本最接近工作的是取出plt.tight_layout,然后必须在代码末尾添加plt.ioff()plt.show()。你能仔细检查你的并提出其他建议吗?我还发现,在这种情况下,如果没有 plt.draw()raw_input() - @alexmcf,我会得到相同的结果
    • 好吧,在我发布我的答案之前,我已经将plt.tight_layout() 移到了顶部 - 没有意识到它会搞砸情节!查看我编辑的版本,我在最后做plt.tight_layout()...对于您的最终版本,删除所有plt.draw()raw_input()plt.ion() 并在末尾附加plt.show()。后者只是为了为您确认情节正常...
    • 确实如此。对不起,我周末不在。再次感谢!
    猜你喜欢
    • 2015-01-09
    • 2019-08-22
    • 1970-01-01
    • 2020-07-14
    • 2022-01-19
    • 2014-03-24
    • 2019-11-08
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多