Histogram update in a for loop with matplotlib.pylab

走远了吗. 提交于 2020-06-14 07:53:45

问题


I am trying to update in for loop a histogram data. but I don't know how to make it. I tried with set_data but it is not working. here is the code:

plt.ion()
ax=plt.subplot(111)
[n,X, V]=ax.hist(range(MAX_X),bins=33,normed=True)

....

alternative=defaultdict(list)
...



for z in range(0,max(alternative)):
stat=zeros(33,int)
for i in range(len(alternative[z])):
    stat[alternative[z][i]]+=1

[n,X, V].set_data(stat)// problem here!!!!!!!
plt.draw()

回答1:


So the problem comes from the fact that [n,X,V] is a list with no set_data method. As far as I am aware, there is no easy way to "update" a histogram in the way you describe without manually reordering and organising the underlying Patches objects.

You would be just as well clearing the axis are replotting each time:

This:

[n,X, V].set_data(stat)// problem here!!!!!!!
plt.draw()

becomes:

ax.cla()
[n,X, V]=ax.hist(stat,bins=33,normed=True)
plt.draw()

Assuming that stat is an array that you want to histogram.



来源:https://stackoverflow.com/questions/23247154/histogram-update-in-a-for-loop-with-matplotlib-pylab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!