【问题标题】:Bar graph in subplot2gridsubplot2grid 中的条形图
【发布时间】:2017-09-08 16:47:20
【问题描述】:

我想要几个子图,其中两个显示来自视频源的帧,第三个显示计算结果作为条形图。 创建 matplotlib 图形后,我创建了几个 subplot2grids,然后使用 FuncAnimation 对其进行更新。

创建条形图(待更新)的常用方法是:

fig = plt.figure()
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
rects = plt.bar(res_x, res_y, color='b')

def animate(args):
    ...
    ...
    for rect, yi in zip(rects, results):
        rect.set_height(yi*100)
    return rects

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

我现在正在尝试在其他子图旁边添加一个条形图:

fig = plt.figure()
plt1 = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
plt2 = plt.subplot2grid((2, 2), (0, 1))

#Confusion with the following
bar_plot = plt.subplot2grid((2,2), (1,1))
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
rects = plt.bar(res_x, res_y, color='b')


def animate(args):
    ...
    ...

    im1 = plt1.imshow(...)
    im2 = plt2.imshow(...)

    for rect, yi in zip(rects, results):
        rect.set_height(yi*100)
    return im1, im2, rects

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

我收到以下错误:AttributeError: 'BarContainer' object has no attribute 'set_animated'

有什么想法可以将条形图“放置”为子图,并与子图中的其他数据一起更新吗?

谢谢!

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    错误来自return im1, im2, rects 行。

    在工作解决方案中,您拥有return rects,即您返回具有set_animated 方法的艺术家列表。在失败的代码中,您有一个 BarContainer 和两个艺术家的元组。正如错误提示的那样,AttributeError: 'BarContainer' object has no attribute 'set_animated'

    一种解决方案可能是生成 BarContainer 的内容列表,您可以将其连接到其他两个艺术家。

    return [rect for rect in rects]+[im1, im2]
    

    一个完整的工作示例:

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    res_x, res_y = [1,2,3], [1,2,3]
    
    fig = plt.figure()
    ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
    ax2 = plt.subplot2grid((2, 2), (0, 1))
    ax3 = plt.subplot2grid((2,2), (1,1))
    
    rects = ax3.bar(res_x, res_y, color='b')
    im1 = ax.imshow([[1,2],[2,3]], vmin=0)
    im2 = ax2.imshow([[1,2],[2,3]], vmin=0)
    
    def animate(i):
    
        im1.set_data([[1,2],[(i/100.),3]])
        im2.set_data([[(i/100.),2],[2.4,3]])
    
        for rect, yi in zip(rects, range(len(res_x))):
            rect.set_height((i/100.)*(yi+0.2))
        return [rect for rect in rects]+[im1, im2]
    
    anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
    plt.show()
    

    【讨论】:

    • @dtam 如果这回答了你的问题,你应该考虑accepting它。如果没有,您可以通过更新您的问题来提供更多详细信息。如果答案有帮助(如果它解决了问题,大概是这样),您也可以考虑投票。当然,您的老问题也是如此(herehere)。
    • 使用这个解决方案我得到以下错误:“AttributeError: draw_artist can only be used after a initial draw which caches the render” 此外,这会打开条形图的绘图,但涵盖了其他两个子图,而我希望所有三个子图都成为子图并将它们放在一起。任何想法?我很抱歉没有接受以前的那些,我现在就去找他们。
    • 由于您没有显示完整的代码,因此无法知道此错误来自何处。但是我在答案中添加了minimal reproducible example,这表明它按预期工作。
    • 您提供的示例让我能够解决我的问题,谢谢!我有一个后续问题:我试图像这样定义条形图轴: ax = plt.axes(xlim=(0, 9), ylim=(0, 100)) 我意识到当我得到错误时我用这条线运行动画。有没有另一种方法可以设置我的 X 轴从 0-9 和 Y 轴从 0-100?干杯!
    • 为了设置现有轴的限制ax,请执行ax.set_xlim(0,9) ax.set_ylim(0,100)
    猜你喜欢
    • 2016-12-09
    • 2018-02-08
    • 2017-10-14
    • 1970-01-01
    • 2017-12-23
    • 2018-08-22
    • 2015-04-10
    • 2023-01-21
    相关资源
    最近更新 更多