当前位置: 首页 > 面试题库 >

subplot2grid中的条形图

慕俊迈
2023-03-14
问题内容

我想要几个子图,其中两个显示来自视频提要的帧,第三个显示以条形图显示的计算结果。创建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’对象没有属性’set_animated’

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

谢谢!


问题答案:

错误来自线路return im1, im2, rects

在工作解决方案中,您有return rects,即您返回具有set_animated方法的艺术家列表。在失败的代码中,您将拥有一个BarContainer和两个艺术家的元组。如错误所示,
AttributeError:’BarContainer’对象没有属性’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()


 类似资料:
  •  模块提供了 subplot2grid() ,该函数能够在画布的特定位置创建 axes 对象(即绘图区域)。不仅如此,它还可以使用不同数量的行、列来创建跨度不同的绘图区域。与 subplot() 和 subplots() 函数不同,subplot2gird() 函数以非等分的形式对画布进行切分,并按照绘图区域的大小来展示最终绘图结果。 函数语法格式如下: plt.subplot2grid(shap

  • 我使用chart.js在我的网页上有一个BarChart。 我在其中添加了两个数据点 现在我想要更新那些条A和B,而不是删除它们并再次添加它们(我已经想好了如何做到这一点)。我想让它们垂直移动以适应它们的新值,但我无法找到如何访问图表中已经存在的数据。 没有什么比得上 其中第一个值将是存储数据的索引(F.E.)。 我该怎么做呢?

  • 我使用的是Chart.js2.5.0版本,需要知道是否有任何方法可以隐藏分组堆叠条形图中每组的空条?图表数据集中的某些数据值可以为空。 这里是我想要的:组合图表类型(分组和堆叠) 小提琴:https://jsfidle.net/q_sabawoon/atlxlg7x/ 请帮帮忙。

  • 我正在尝试使用matplotlib创建条形图。 x轴数据是带有年份的列表:[19501960、19701、980、19901、995-2015] y轴数据是一个数量与年数相等的列表。 这是我的代码: 结果是:条与条之间有太多的空间 正如你所看到的,1950-1960年之间的差距是巨大的。我怎么能做到这一点,这样1950-1995年酒吧之间就没有间隙。我知道它的间隔为10年,但看起来并不好。 任何帮

  • 问题内容: 我在Matplotlib中制作条形图,调用如下: 有没有一种方法可以在Matplotlib中使用此功能来实现? 问题答案: 将添加为关键字参数。例如 。 这将垂直填充条形间隙。

  • 我用Matplotlib做了一个条形图,调用如下: 我得到一个条形图,看起来像这样: 我想要的是在连续的条之间没有空白,例如,更像这样: 有没有办法在Matplotlib中使用函数来实现这一点?