bar的参考链接:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.bar.html

第一种办法

一种方法是每次都重新画,包括清除figure

def animate(fi):
bars=[]
if len(frames)>fi:
# axs.text(0.1,0.90,time_template%(time.time()-start_time),transform=axs.transAxes)#所以这样
time_text.set_text(time_template%(0.1*fi))#这个必须没有axs.cla()才行
# axs.cla()
axs.set_title('bubble_sort_visualization')
axs.set_xticks([])
axs.set_yticks([])
bars=axs.bar(list(range(Data.data_count)),#个数
[d.value for d in frames[fi]],#数据
1, #宽度
color=[d.color for d in frames[fi]]#颜色
).get_children()
return bars
anim=animation.FuncAnimation(fig,animate,frames=len(frames), interval=frame_interval,repeat=False)

 

这样效率很低,而且也有一些不可取的弊端,比如每次都需要重新设置xticks、假如figure上添加的有其他东西,这些东西也一并被clear了,还需要重新添加,比如text,或者labale。

第二种办法

参考链接:https://stackoverflow.com/questions/16249466/dynamically-updating-a-bar-plot-in-matplotlib

这个链接里的内容和上面的差不多:https://stackoverflow.com/questions/34372021/python-matplotlib-animate-bar-and-plot-in-one-picture/34372367#34372367

可以像平时画线更新data那样来更新bar的高

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation fig=plt.figure(1,figsize=(4,3))
ax=fig.add_subplot(111)
ax.set_title('bar_animate_test')
#ax.set_xticks([])注释了这个是能看到变化,要不看不到变化,不对,能看到变化,去了注释吧
#ax.set_yticks([])
ax.set_xlabel('xlable')
N=5
frames=50
x=np.arange(1,N+1) collection=[]
collection.append([i for i in x])
for i in range(frames):
collection.append([ci+1 for ci in collection[i]])
print(collection)
xstd=[0,1,2,3,4]
bars=ax.bar(x,collection[0],0.30)
def animate(fi):
# collection=[i+1 for i in x]
   ax.set_ylim(0,max(collection[fi])+3)#对于问题3,添加了这个
for rect ,yi in zip(bars,collection[fi]):
rect.set_height(yi)
# bars.set_height(collection)
return bars
anim=animation.FuncAnimation(fig,animate,frames=frames,interval=10,repeat=False)
plt.show()

  

  

问题

  *)TypeError: 'numpy.int32' object is not iterable

x=np.arange(1,N+1)
collection=[i for i in x]
#collection=[i for i in list(x)]#错误的认为是dtype的原因,将这里改成了list(x)
for i in range(frames):
collection.append([ci+1 for ci in collection[i]])#问题的原因是因为此时的collection还是一个一位数组,所以这个collection[i]是一个x里的一个数,并不是一个列表,我竟然还以为的dtype的原因,又改了
xstd=[0,1,2,3,4]

  应该是

collection=[]
collection.append([i for i in x])#成为二维数组
for i in range(frames):
collection.append([ci+1 for ci in collection[i]])

  然后又出现了下面的问题:

  *)TypeError: only size-1 arrays can be converted to Python scalars

Traceback (most recent call last):
File "forTest.py", line 22, in <module>
bars=ax.bar(x,collection,0.30)
File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\__init__.py", line 1589, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
label='_nolegend_',
File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 707, in __init__
Patch.__init__(self, **kwargs)
File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 89, in __init__
self.set_linewidth(linewidth)
File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 368, in set_linewidth
self._linewidth = float(w)
TypeError: only size-1 arrays can be converted to Python scalars

  参考链接:https://www.cnblogs.com/Summerio/p/9723099.html

  应该是传递的参数错误,仔细想了一下,在报错的代码行中,collection原来是没错的,因为原来是一维数组,现在变成二维了,改为

bars=ax.bar(x,collection[0],0.30)

  好了

  *)出现的问题,在上面的代码中,运行的时候不会画布的大小不会变,会又条形图溢出的情况,在animate()中添加了

def animate(fi):
# collection=[i+1 for i in x]
ax.set_ylim(0,max(collection[fi])+3)#添加了这个
for rect ,yi in zip(bars,collection[fi]):
rect.set_height(yi) # bars.set_height(collection)
return bars

  

  

别的属性

  *)条形图是怎样控制间隔的:

  是通过控制宽度

width=1,#没有间隔,每个条形图会紧挨着

  *)errorbar:

  是加一个横线,能通过xerr和yerr来调整方向

xstd=[0,1,2,3,4]
bars=ax.bar(x,collection,0.30,xerr=xstd)

  

最新文章

  1. JAVA学习笔记之与C#对比
  2. 利用netperf、iperf、mtr测试网络
  3. JQuery------如何判断当前点击的是否是哪个类
  4. chromium的部署工具depot_tools和gclient
  5. 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】
  6. fopen中的mode(20161115)
  7. Linux,activemq-cpp之消息过滤器
  8. 原生addClass 方法 添加类函数
  9. c++ 读取不了hdf5文件中的字符串
  10. Docker 错误 docker: invalid reference format. 的解决
  11. iOS保持待续连接
  12. 数据统计 任务的一点感想 , sql 使用中的坑。
  13. 组合拳出击-Self型XSS变废为宝
  14. DOM之城市二级联动
  15. git push 报错:missing Change-Id in commit message footer
  16. C:基础知识
  17. 网易的Airtest
  18. Tensorflow函数——tf.variable_scope()
  19. INSTALL_FAILED_CONFLICTING_PROVIDER
  20. Linux下架构高可用性网络----HA+LB+lvs

热门文章

  1. Redis数据库详解
  2. js闭包和原型链好文
  3. i++和++1
  4. Python查看帮助---help函数
  5. 13-numpy笔记-莫烦pandas-1
  6. 测试脚本中的等待方法 alter对话框处理
  7. C++17尝鲜
  8. 基于Distiller的模型压缩工具简介
  9. 怎么写Java项目?
  10. 实现 Cloneable 需要注意