在Python中对直方图序列进行动画处理

时间:2020-07-02 14:23:25

标签: python animation histogram

我收集了约150个数据列表。我想用直方图表示每个列表。我正在尝试对集合中每个数据列表的这些直方图进行动画处理,但是我对如何做到这一点感到困惑。

例如,这是收藏集中的5个列表:

string dataPath = @"C: \Users\....;

我基本上复制并粘贴了此演示中的代码:https://matplotlib.org/3.1.1/gallery/animation/animated_histogram.html 并在我的相同代码的实现中将100更改为10:

 data = [[86.3, 61.3, 36.9, 54, 79, 104, 61.3, 36.3, 11.9, 29],
 [83.45, 58.45, 34.8, 51.25, 76.25, 101.25, 58.45, 33.45, 9.8, 26.25],
 [81.46, 56.46, 33.43, 49.06, 74.06, 99.06, 56.46, 31.46, 8.43, 24.06],
 [79.65, 54.65, 32.225, 47.3, 72.3, 97.3, 54.65, 29.65, 7.225, 22.3],
 [78.02, 53.02, 31.18, 45.96, 70.96, 95.96, 53.02, 28.02, 6.18, 20.96]]

输出是一个很好的直方图,但是我期望import matplotlib.patches as patches import matplotlib.path as path import matplotlib.animation as animation n, bins = np.histogram(data, 10) # get the corners of the rectangles for the histogram left = np.array(bins[:-1]) right = np.array(bins[1:]) bottom = np.zeros(len(left)) top = bottom + n nrects = len(left) nverts = nrects * (1 + 3 + 1) verts = nrects * (1 + 3 + 1) verts = np.zeros((nverts, 2)) codes = np.ones(nverts, int) * path.Path.LINETO codes[0::5] = path.Path.MOVETO codes[4::5] = path.Path.CLOSEPOLY verts[0::5, 0] = left verts[0::5, 1] = bottom verts[1::5, 0] = left verts[1::5, 1] = top verts[2::5, 0] = right verts[2::5, 1] = top verts[3::5, 0] = right verts[3::5, 1] = bottom def animate(i): # simulate new data coming in n, bins = np.histogram(data, 10) top = bottom + n verts[1::5, 1] = top verts[2::5, 1] = top return [patch, ] fig, ax = plt.subplots() barpath = path.Path(verts, codes) patch = patches.PathPatch( barpath, facecolor='green', edgecolor='yellow', alpha=0.5) ax.add_patch(patch) ax.set_xlim(left[0], right[-1]) ax.set_ylim(bottom.min(), top.max()) ani = animation.FuncAnimation(fig, animate, 10, repeat=False, blit=True) plt.show() 中所有列表的动画,每个列表的直方图。如何使用此代码生成实际的动画?还是有更简单的方法可以完全做到这一点?

0 个答案:

没有答案