1.图

2.代码

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
#导出数据,当然这个数据也可以是直接在网上下载,是的有点慢
#网址:https://gist.githubusercontent.com/johnburnmurdoch/4199dbe55095c3e13de8d5b2e5e5307a/raw/fa018b25c24b7b5f47fd0568937ff6c04e384786/city_populations
df = pd.read_csv('city_populations.csv',
usecols=['name', 'group', 'year', 'value']) #将数据下载下来放在指定默认的目录和文件夹下,city_populations.csv
#定义
current_year = 2018
dff=() fig, ax = plt.subplots(figsize=(15, 8)) colors = dict(zip(
['India', 'Europe', 'Asia', 'Latin America',
'Middle East', 'North America', 'Africa'],
['#adb0ff', '#ffb3ff', '#90d595', '#e48381',
'#aafbff', '#f7bb5f', '#eafb50']
)) group_lk = df.set_index('name')['group'].to_dict() def draw_barchart(year):
dff = df[df['year'].eq(year)].sort_values(by='value', ascending=True).tail(10)
ax.clear() #每次清空、刷新
ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']])
dx = dff['value'].max() / 200
for i, (value, name) in enumerate(zip(dff['value'], dff['name'])):
ax.text(value-dx, i, name, size=14, weight=600, ha='right', va='bottom')
ax.text(value-dx, i-.25, group_lk[name], size=10, color='#444444', ha='right', va='baseline')
ax.text(value+dx, i, f'{value:,.0f}', size=14, ha='left', va='center')
#显示文字,x=0,y=1.10,坐标,ha=水平对准=水平线平放
#ax.text()格式=(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")
#string=字符串='文字内容'
ax.text(0, 1.10, 'The most populous cities in the world from 1968 to 2018',
transform=ax.transAxes, size=18, weight=600, ha='left') #文字标题,第1层
ax.text(0, 1.04, 'Population (thousands)', transform=ax.transAxes, size=12, color='#777777') #显示文字,第2层
ax.text(1, 0.4, year, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800) #右边固定显示动图年份
#va=verticalalignment="top",垂直对准
#ha=horizontalalignment="right",alignment=对准,水平对准
ax.xaxis.set_ticks_position('top') #x轴在上面 ax.set_yticks([]) #默认是显示y轴的名称,左边垂直的城市名字,设为[]就是不显示
ax.margins(0, 0.01) #不设置就是默认值,缩放比例(0,0.05)
ax.grid(which='major', axis='x', linestyle='--') #垂直线,布局和格式
ax.set_axisbelow(True) #默认是true的 plt.box(False) #默认是True,False之后不显示黑色线框 animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1968, 2019)) #以animator形式展现动画
plt.show() #以plt的形式展现图片

python的matplotlib的热门可视化动图的更多相关文章

  1. 手把手教你做一个python+matplotlib的炫酷的数据可视化动图

    1.效果图 2.注意: 上述资料是虚拟的,为了学习制作动图,构建的. 仅供学习, 不是真实数据,请别误传. 当自己需要对真实数据进行可视化时,可进行适当修改. 3.代码: #第1步:导出模块,固定 i ...

  2. 基于matplotlib的数据可视化 - 热图imshow

    热图: Display an image on the axes. 可以用来比较两个矩阵的相似程度 mp.imshow(z, cmap=颜色映射,origin=垂直轴向) imshow( X, cma ...

  3. python中matplotlib绘图封装类之折线图、条状图、圆饼图

    DrawHelper.py封装类源码: import matplotlib import matplotlib.pyplot as plt import numpy as np class DrawH ...

  4. python下matplotlib的subplot的多图显示位置的问题

    1.说明 1.1 多图: 221,222 212 ------------附最后讲解,这下更清楚了吧,取个名字:颠倒一下--- 1.2 多图 211 223,224 ------------附最后讲解 ...

  5. Python中matplotlib.pyplot.imshow画灰度图的多种方法

    转载:https://www.jianshu.com/p/8f96318a153f matplotlib库的教程和使用方法此处就不累赘了,网上有十分多优秀的教程资源.此处直接上代码: def demo ...

  6. gganimate|创建可视化动图,让你的图表会说话

    本文首发于“生信补给站”公众号,https://mp.weixin.qq.com/s/kKQ2670FBiDqVCMuLBL9NQ 更多关于R语言,ggplot2绘图,生信分析的内容,敬请关注小号. ...

  7. 利用Python生成GIF动图

    一.PIL库 1.PIL库的概括: PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库 ...

  8. python学习之matplotlib绘制动图(FuncAnimation()参数)

    1.函数FuncAnimation(fig,func,frames,init_func,interval,blit)是绘制动图的主要函数,其参数如下: a.fig 绘制动图的画布名称 b.func自定 ...

  9. Python数据分析matplotlib可视化之绘图

    Matplotlib是一个基于python的2D画图库,能够用python脚本方便的画出折线图,直方图,功率谱图,散点图等常用图表,而且语法简单. Python中通过matplotlib模块的pypl ...

随机推荐

  1. 网络https工作原理

    网络https工作原理 待办 https://www.runoob.com/w3cnote/https-ssl-intro.html

  2. DVWA全级别之Insecure CAPTCHA(不安全的验证码)

    Insecure CAPTCHA Insecure CAPTCHA,意思是不安全的验证码,CAPTCHA是Completely Automated Public Turing Test to Tell ...

  3. 转载:EQ--biquad filter

    http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt https://arachnoid.com/BiQuadDesigner/index.html ...

  4. Linux(Ubuntu)服务器是否安装ssh,使用xshell远程连接

    1.查看 ssh 是否启动,如果有 sshd 说明已经启动 sudo ps -e | grep ssh 2.启动 ssh 服务 sudo service ssh start 3.如果第二步没有成功启动 ...

  5. win10图标变白的解决办法(亲测有用)

    1.首先,随便打开一个文件夹点击[查看]菜单,然后勾选[隐藏的项目]: 2.同时按下快捷键[Win]+[R],在打开的[运行]窗口中输入%localappdata%: 3.在打开的文件夹中,找到[Ic ...

  6. Docker 基本命令和使用

    Docker 基本命令 systemctl start docker : 启动 Docker systemctl stop docker : 停止 Docker systemctl restart d ...

  7. QQ发起聊天

    QQ推广 网址: http://shang.qq.com/v3/widget.html 一键加群 实例: <a target="_blank" href="//sh ...

  8. P2023 [AHOI2009]维护序列 区间加乘模板

    题意: 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式:N<=1e5(1)把数列中的一段数全部乘一个值;(2)把数列中的一段数全部加一个值;(3)询问数列中的一段数的和,由于 ...

  9. py1

    python 下载安装  https://python.org python解释性语言 python数据结构 *输入输出 print(12,34,56,end='',sep='*') input() ...

  10. xampp 配置多域名工程 解决 Access forbidden!

    XAMPP虚拟主机配置,多域名绑定访问本地站点 XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. ...