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)

  

Python 绘图与可视化 matplotlib 动态条形图 bar的更多相关文章

  1. Python 绘图与可视化 matplotlib 制作Gif动图

    参考链接:https://blog.csdn.net/theonegis/article/details/51037850 官方文档:https://matplotlib.org/3.1.0/api/ ...

  2. Python 绘图与可视化 matplotlib(下)

    详细的参考链接:更详细的:https://www.cnblogs.com/zhizhan/p/5615947.html 图像.子图.坐标轴以及记号 Matplotlib中图像的意思是打开的整个画图窗口 ...

  3. Python 绘图与可视化 matplotlib(上)

    参考链接:https://www.cnblogs.com/dudududu/p/9149762.html 更详细的:https://www.cnblogs.com/zhizhan/p/5615947. ...

  4. Python 绘图与可视化 matplotlib text 与transform

    Text 为plots添加文本或者公式,反正就是添加文本了 参考链接:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html#ma ...

  5. Python 绘图与可视化 matplotlib 散点图、numpy模块的random()

    效果: 代码: def scatter_curve(): # plt.subplot(1,1,1) n=1024 X=np.random.normal(0,1,n) Y=np.random.norma ...

  6. Python 绘图与可视化 matplotlib 填充fill和fill_between

    参考链接:https://blog.csdn.net/You_are_my_dream/article/details/53457960 fill()填充函数曲线与坐标轴之间的区域: x = np.l ...

  7. 【疫情动态条形图】用Python开发全球疫情排名动态条形图bar_chart_race

    一.开发背景 你好,我是 @马哥python说 ,这是我用Python开发的全球疫情动态条形图,演示效果: https://www.zhihu.com/zvideo/15603276220259696 ...

  8. Python绘图与可视化

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  9. IPython绘图和可视化---matplotlib 入门

    最近总是需要用matplotlib绘制一些图,由于是新手,所以总是需要去翻书来找怎么用,即使刚用过的,也总是忘.所以,想写一个入门的教程,一方面帮助我自己熟悉这些函数,另一方面有比我还小白的新手可以借 ...

随机推荐

  1. Appium+python自动化(三)- SDK Manager(超详解)

    简介 本来宏哥一开始打算用真机做的,所以在前边搭建环境时候就没有下载SDK,但是由于许多小伙伴通过博客发短消息给宏哥留言说是没有真机,所以顺应民意整理一下模拟器,毕竟“得民心者,得天下”.SDK顾名思 ...

  2. 为什么要使用token,token与session区别是什么

    目录 一.session的状态保持及弊端 二.token认证机制 一.session的状态保持及弊端 当用户第一次通过浏览器使用用户名和密码访问服务器时,服务器会验证用户数据,验证成功后在服务器端写入 ...

  3. 创建vector<T>容器

    vector<T> 容器是包含 T 类型元素的序列容器,和 array<T,N> 容器相似,不同的是 vector<T> 容器的大小可以自动增长,从而可以包含任意数 ...

  4. [POJ1189][BZOJ1867][CODEVS1709]钉子和小球

    题目描述 Description 有一个三角形木板,竖直立放,上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周围的钉子的距离都等于d,每个格子的宽度也都等于d,且 ...

  5. Linux性能优化实战学习笔记:第三讲

    一.关于上下文切换的几个为什么 1. 上下文切换是什么? 上下文切换是对任务当前运行状态的暂存和恢复 2. CPU为什么要进行上下文切换? 当多个进程竞争CPU的时候,CPU为了保证每个进程能公平被调 ...

  6. MySQL实战45讲学习笔记:第十四讲

    一.引子 在开发系统的时候,你可能经常需要计算一个表的行数,比如一个交易系统的所有变更记录总数.这时候你可能会想,一条 select count(*) from t 语句不就解决了吗? 但是,你会发现 ...

  7. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  8. [LeetCode] 62. Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. thinkphp5.0学习(九):TP5.0视图和模板

    原文地址:http://blog.csdn.net/fight_tianer/article/details/78602711 一.视图 1.加载页面 1.继承系统控制器类 return $this- ...

  10. GreenPlum 大数据平台--基础使用(二)

    连接参数 连接参数 描述 环境变量 应用名称 连接到数据库的应用名称,保存在application_name连接参数中.默认值是psql. $PGAPPNAME 数据库名 用户想要连接的数据库名称.对 ...