【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)

目录

  • 专栏说明
    • 一、效果图展示
      • 1.1 水平条形图
      • 1.2 竖直条形图
      • 2. 折线图
      • 3. 散点图
      • 4.饼状图
      • 5. 气泡图
      • 6.1 地理空间点图
      • 6.2 多边形地理空间图
      • 7.多个图表
      • 8.城市人口
      • 9.G7国家的预期寿命
    • 10. 新南威尔士州 COVID 可视化
    • 二、专栏学习说明
      • 资源下载
    • API汇总
    • 三、环境配置与检测
      • 使用我的venv环境
      • test检测配置是否成功
    • 四、数据集说明
    • 五、常见问题
      • 1.gif动图生成等待
      • 2.图中出现中文
      • 3.参数最多15个的限制取消方法
      • 4.Animation was deleted without rendering anything.不影响生成动图
      • 5. 关于安装库失败,比如说无法安装geopandas和contextily
      • 6.地理空间图无法添加title
      • 7.TypeError: 'MovieWriterRegistry' object is not an iterator
    • 1.条形图
      • 条形图API说明:
      • 条形图数据集
      • 1.1 水平条形图例程
      • 1.2 竖直条形图例程
    • 2.折线图
      • 折线图API说明:
      • 折线图数据集
      • 折线图例程
    • 3.散点图
      • 散点图API说明:
      • 散点图数据集
      • 散点图例程
    • 4.饼状图
      • 饼状图API说明:
      • 饼状图数据集
      • 饼状图例程
    • 5.气泡图
      • 气泡图API说明:
      • 气泡图数据集
      • 气泡图例程
    • 6.地理空间图
    • 6.1 地理空间点图
      • 地理空间点图API说明:
      • 地理空间点图数据集
      • 地理空间点图例程
    • 6.2 多边形地理空间图
      • 多边形地理空间图API说明:
      • 多边形地理空间图数据集
      • 多边形地理空间图例程
    • 7.多个图表
      • 多个图表API说明:
      • 多个图表数据集
      • 多个图表例程
    • 8.城市人口
      • 城市人口API说明:
      • 城市人口数据集
      • 城市人口图例程
    • 9.G7国家的预期寿命
      • G7国家的预期寿命图API说明:
      • G7国家的预期寿命图数据集
      • G7国家的预期寿命图例程
    • 10.新南威尔士州 COVID 可视化
      • 新南威尔士州 COVID 可视化API说明:
      • 新南威尔士州 COVID 可视化数据集
      • 新南威尔士州 COVID 可视化例程
    • 总结


大家觉得有用的话,给个,点个关注,放进收藏夹吃灰,我会继续给大家分享学习笔记!

版权声明:

发现你走远了@mzh原创作品,转载必须标注原文链接

Copyright 2022 mzh

Crated:2022-1-13

欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中

专栏说明

【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)

折腾了一星期,梳理了很多入门小白避雷的方法,还会继续更新,如果看了对你有帮助,希望得到大家的点赞收藏支持!(毕竟时短间学完太难了,建议放进收藏夹吃灰

本专栏学习笔记基于JackMcKew开发的pandas_alive这一开源项目。

  • 实际运行中原来的项目从网络上获取数据集有时候因为网络原因会失败,我进行了调整,从网络读取数据集的部分改为下载使用,更稳定。(作者的初衷应该是为了能够即时更新)
  • 很多微信公众号都是直接把JackMcKew的GitHub的readme直接贴过来,我怀疑他们根本没有自己运行过,我看很多地方都是你抄我,我抄你,抄来抄去一模一样但是都没有个中文文档注释说明,也没说怎么解决我们国人自己使用时需要加入中文等要求,我花了一个星期琢磨总算明白了一些使用的说明。
  • 本文是我自己写的初学者入门学习笔记,欢迎大家批评指正,欢迎评论区留言进行学习讨论。(没有找到中文说明文档有些参数我是自己慢慢改参数试出来的,如果理解有误请见谅)。
  • 有部分图我怀疑是数据对不上了,比说10.新南威尔士州疫情 COVID 可视化,获取数据的接口出现了问题,我暂时没有什么能用的数据,大家有兴趣找数据可以尝试。
  • 气泡图要安装迭代器,额外配置安装ffmpeg安装ffmpeg的安装配置和python中使用ffmpy(保姆级图文)

一、效果图展示

如果图片空着,说明我还没有画出来,环境配置没到位。

1.1 水平条形图

1.2 竖直条形图

2. 折线图

图片为了能上传经过压缩,你自己生成的效果应该更好一点!

3. 散点图

图片为了能上传经过压缩,你自己生成的效果应该更好一点!
在这里插入图片描述

4.饼状图

5. 气泡图

6.1 地理空间点图

6.2 多边形地理空间图

7.多个图表

8.城市人口

9.G7国家的预期寿命

10. 新南威尔士州 COVID 可视化

数据集接口异常,使用了项目作者的原图。(不是我绘制的)


二、专栏学习说明

【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)

折腾了一星期,梳理了很多入门小白避雷的方法,还会继续更新,如果看了对你有帮助,希望得到大家的点赞收藏支持!(毕竟时短间学完太难了,建议放进收藏夹吃灰

本专栏学习笔记基于JackMcKew开发的pandas_alive这一开源项目。

  • 实际运行中原来的项目从网络上获取数据集有时候因为网络原因会失败,我进行了调整,从网络读取数据集的部分改为下载使用,更稳定。(作者的初衷应该是为了能够即时更新)
  • 很多微信公众号都是直接把JackMcKew的GitHub的readme直接贴过来,我怀疑他们根本没有自己运行过,我看很多地方都是你抄我,我抄你,抄来抄去一模一样但是都没有个中文文档注释说明,也没说怎么解决我们国人自己使用时需要加入中文等要求,我花了一个星期琢磨总算明白了一些使用的说明。
  • 本文是我自己写的初学者入门学习笔记,欢迎大家批评指正,欢迎评论区留言进行学习讨论。(没有找到中文说明文档有些参数我是自己慢慢改参数试出来的,如果理解有误请见谅)。
  • 有部分图我怀疑是数据对不上了,比说10.新南威尔士州疫情 COVID 可视化,获取数据的接口出现了问题,我暂时没有什么能用的数据,大家有兴趣找数据可以尝试。
  • 气泡图要安装迭代器,额外配置安装ffmpeg安装ffmpeg的安装配置和python中使用ffmpy(保姆级图文)

资源下载

数据集合测试代码的文件都放在码云上

https://gitee.com/miao-zehao/pandas-live-game-graph

在这里插入图片描述

API汇总


三、环境配置与检测

在工作目录下一定要有必要的文件,框选的内容都是不可缺少的。
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第1张图片

使用我的venv环境

不会使用venv,可以参考我的说明文章虚拟环境的安装和配置-virtualenv 合集(有pycharm图形化界面操作+无pycharm命令行操作的保姆级图文)

或者使用我提供的最新可用requirement.txt(原GitHub问题比较多)

【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第2张图片
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第3张图片

本来要安装必要的库,原作者给的requirements.txt有些包的版本冲突比如urllib3库版本号区别
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第4张图片
还有作者的requirements中没有提到但是实际上用到的(属实让我debug了好久)
PIL库(python3已经改名为Pillow库,安装Pillow就可以了)和tqdm库还有在绘制地理空间图时要安装descartes库
我都帮大家装好了虚拟环境venv直接下载使用,你不需要进行繁琐的第三方库安装了,建议直接用我资源里面的venv虚拟环境

test检测配置是否成功

作者提供了三个test程序,如果检测pass说明你的环境配置是ok的

  1. test_base.py
    测试能不能获取在线数据集,这个可有可无,偶尔通过偶尔通不过,因为是GitHub上面的数据,网络问题懂得都懂。
    【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第5张图片

  2. test_plot.py
    最重要的test!决定能不能出图
    【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第6张图片

  3. test_charts.py
    这个报错问题不大,可能是因为作者的测试文件命名的问题
    【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第7张图片

  4. 关键!运行完3个测试文件后能不能生成这个test.gif图
    【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第8张图片
    test.gif
    【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第9张图片

四、数据集说明

  • 读取得到的数据是pandas库下的DataFrame格式
  • 有一列数据是时间数据,一般都是第0列是时间数据,表示一些日期时间,是竞赛动图的时间轴
    时间能够被pd.read_csv读取csv数据时候的parse_dates参数解析,类似格式如下:
    • 2022-1-12
    • 2022/1/22
  • 我在绘制竖直条形图时,当我的数据参数数量大于15个,会报错UserWarning: Plotting too many bars may result in undesirable output, use n_visible=15 to limit number of bars warnings.warn(,要求15个之内。
    在这里插入图片描述
  • 数据集不能有中文,如果要有中文,需要额外自己导入中文字体。
######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
##################################################

打印一份可用的数据集供大家参考
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第10张图片
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第11张图片

本专栏读取数据一般习惯用pd. read_csv,可阅读 https://blog.csdn.net/weixin_44056331/article/details/89366105 得到更多关于读取csv方法的api说明,后面专栏的文章不再赘述读取数据的问题,一些简单常用的参数说明:

index_col=0
表示第0列为index值在上面的数据中就是时间,建议把时间放第0列

parse_dates=[0]
表示把第0列解析为时间格式内容

thousands=’,’
表示千分位用“,”间隔


五、常见问题

本栏目会不断更新,收集评论区的问题,一起讨论避坑。

1.gif动图生成等待

图中第一行显示的是竞赛图中每位竞赛者。

生成动图中,数据量越大生成时间越长,需要耐心等待(我一开始学习的时候还以为运行失败了,运行到一半,一直重复运行,一直出不了图,还卡死了·······这个时候要耐心等待,看你电脑配置)
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第12张图片
生成成功,如果飘红报错那就是有问题了
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第13张图片


2.图中出现中文

需要导入中文字体

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

因为用下载好的线下数据集代替了线上数据集,读取需要用到pands库

import pandas as pd

3.参数最多15个的限制取消方法

在开发者的源文件看到,为了防止渲染图片死机,对于属性限制15个,讲道理改动这个,你的电脑配置好一点,应该是能绘制更多属性的。(我电脑不太行,我没试过!)
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第14张图片


4.Animation was deleted without rendering anything.不影响生成动图

其实很离谱,我本来没有报错的,但是安装了geopandas库和geopandas库需要的4个库之后反正莫名其妙开始报错了(也有可能是安装geopandas库的时候升级了部分库导致的)
但是这个只是提醒,不影响出图。

matplotlib\animation.py:889: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you have outputted the Animation using `plt.show()` or `anim.save()`.
  warnings.warn(

这是一个用户警告:即为我们操作不规范导致的警告,它告诉我们FixedFormatter(刻度形式) 只能与 FixedLocator定位器 一起使用,而不能使用其他方法改变刻度形式!!!
图的x轴刻度转换成浮点数,但是不影响生成动图。
在这里插入图片描述


5. 关于安装库失败,比如说无法安装geopandas和contextily

安装geopandas和contextily手把手从零开始(保姆级图文+安装所需的文件下载)

原作者给的requirements.txt一些问题
有些包的版本冲突比如urllib3库版本号区别
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第15张图片

  • Pillow库 作者的requirements中没有提到但是实际上用到的(属实让我debug了好久)
    PIL库(python3已经改名为Pillow库,安装Pillow就可以了)和
  • tqdm库还有在
  • 绘制地理空间图时要安装descartes库
    我都帮大家装好了虚拟环境venv直接下载使用,你不需要进行繁琐的第三方库安装了,还是建议直接用我资源里面的venv虚拟环境

6.地理空间图无法添加title

我发现了项目的bug
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第16张图片
没有标题的·····


7.TypeError: ‘MovieWriterRegistry’ object is not an iterator

If you get an error such as `TypeError: 'MovieWriterRegistry' object is not an iterator`, this signals there isn't a writer library installed on your machine.
如果出现诸如“TypeError:”MovieWriterRegistry“对象不是迭代器”之类的错误,这表示您的计算机上没有安装writer库。
此软件包使用[matplotlib.animation函数](https://matplotlib.org/3.2.1/api/animation_api.html),因此需要编写器库。
  • 气泡图要安装迭代器,额外配置安装ffmpeg
  • 安装ffmpeg的安装配置和python中使用ffmpy(保姆级图文)

1.条形图

条形图API说明:

df.fillna(0).plot_animated(‘1.水平条形图.gif’, period_fmt="%Y-%m-%d",
title=‘发现你走远了——1.水平条形图’, fixed_max=True,
fixed_order=True)
df.fillna(0).plot_animated(参数1, period_fmt="%Y-%m-%d",
title=‘发现你走远了——1.水平条形图’, fixed_max=True,
fixed_order=True)

  • 参数1 生成图片名字.gif

period_fmt="%Y-%m-%d %H:%M:%S"
格式化成2022-01-12 5:30:00形式,可选可省略缺省 表示格式化时间输出在图片的右下角

fixed_max 是否固定背景图中背景条框
True固定背景图中背景条框

false 不固定背景图中背景条框

fixed_order 是否固定竞赛者条目,就是各个竞赛者的位次会不会变化

  • True 固定竞赛者条目
  • false 固定竞赛者条目

orientation=‘h’
orientation=‘v’
条形图的方向是水平还是竖直

  • h 水平 (默认)
  • v 竖直

perpendicular_bar_func=‘max’
增加一条全部数据的阴影部分辅助观看

  • max 阴影部分数值为最大值
  • mean 阴影部分数值为平均值
  • min 阴影部分数值为最小值

条形图数据集

保存为文件Aus_Elec_Gen_1980_2018.csv使用,建议通过码云下载

,  Black coal,  Brown coal,  Natural gas,  Oil products,  Other a,  Biomass,  Wind,  Hydro,Large-scale solar PV,  Small-scale solar PV,  Geothermal
1989,87573.00,33594.00,14359.00,3552.00,,750.00,,14880.00,,,
1990,89511.00,36048.00,10772.00,3396.00,,769.00,,16103.00,,,
1991,94325.10,34559.00,11633.00,2362.00,,670.00,,15768.00,,10.90,
1992,97872.70,33248.00,12295.00,2421.00,,670.00,,16953.00,,13.30,
1993,100544.10,34890.00,12199.00,2320.00,,670.00,4.00,16649.00,,15.90,
1994,102522.10,35832.00,14913.00,2738.00,,723.00,7.00,16239.00,,18.90,
1995,106089.60,39427.00,12445.00,2805.00,,928.00,7.00,15731.00,,23.40,
1996,109452.20,41893.00,11426.00,2175.00,,965.00,7.00,16852.00,,27.80,
1997,116969.50,46633.00,12934.00,1821.00,,1029.00,8.00,15733.00,,33.50,
1998,118586.30,49703.00,16001.00,1729.00,,1133.00,28.00,16563.00,,37.70,
1999,123833.50,50200.00,16245.00,1784.00,,1134.00,58.00,16720.00,,43.50,
2000,134264.00,52223.00,17271.00,2044.00,,645.00,210.00,16933.00,,50.00,
2001,116774.70,56493.00,31730.00,2446.00,,950.00,364.00,16054.00,,58.30,
2002,115296.60,55160.30,29375.30,1661.00,1792.20,1583.60,703.10,16490.00,,58.30,
2003,121182.30,55572.80,30919.40,1388.70,1817.00,1799.50,705.00,16331.10,,68.10,0.50
2004,127788.50,53377.80,23802.90,2841.00,434.00,3830.00,885.00,15612.20,,77.80,0.50
2005,130231.60,54552.80,22725.90,3058.00,517.00,3911.00,1713.10,16029.20,,90.30,0.50
2006,132368.50,54375.80,31849.90,2893.00,479.00,3953.00,2611.10,14517.00,,104.70,0.50
2007,129569.30,54658.70,34955.80,4058.00,106.00,4596.00,3093.10,12056.90,,122.80,0.50
2008,127270.50,56981.40,37660.40,3388.00,3579.90,2795.20,3823.80,11869.40,,155.60,0.50
2009,123724.50,56068.30,44585.40,3106.80,2991.00,2777.40,5051.70,13548.70,,424.90,0.50
2010,116948.80,55298.10,48996.20,3093.80,2716.10,2101.70,6084.90,16806.70,,1530.40,0.50
2011,116654.40,55067.70,48571.60,2702.50,1088.10,3043.75,6969.70,14083.30,,2558.70,0.50
2012,111491.10,47555.10,51053.40,4464.30,1945.30,3143.50,7959.60,18269.60,,3826.30,0.50
2013,106965.50,46076.20,54393.90,5012.40,,3499.39,10252.00,18421.00,49.68,4366.28,0.50
2014,107639.40,50970.30,52916.91,6828.45,,3592.23,11466.60,13445.00,107.22,5424.11,0.60
2015,114294.98,48795.86,50536.08,5655.62,,3789.89,12199.50,15318.18,457.21,6381.05,0.22
2016,118272.30,43557.76,50459.75,5272.59,,3500.55,12596.99,16284.89,672.40,7399.26,0.50
2017,121592.60,36067.00,53839.39,5262.60,,3534.09,14989.42,15838.82,1007.58,8922.36,

1.1 水平条形图例程

# @Time    : 2022/1/12 11:46
# @Author  : 南黎
# @FileName: 1.1水平条形图.py
import pandas as pd
import pandas_alive

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

df = pd.read_csv("数据源data/Aus_Elec_Gen_1980_2018.csv", index_col=0, parse_dates=[0], thousands=',')

df.fillna(0).plot_animated('1.1水平条形图.gif', period_fmt="%Y-%m-%d",
                           title='发现你走远了——1.1水平条形图', fixed_max=False,
                           perpendicular_bar_func='mean',
                           fixed_order=False)


1.2 竖直条形图例程

# @Time    : 2022/1/12 13:24
# @Author  : 南黎
# @FileName: 1.2竖直条形图.py
import pandas_alive
import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

df = pd.read_csv("数据源data/Aus_Elec_Gen_1980_2018.csv", index_col=0, parse_dates=[0], thousands=',')

df.plot_animated(filename='1.2竖直条形图.gif', title='发现你走远了——1.2竖直条形图',perpendicular_bar_func='min', orientation='v')


2.折线图

折线图API说明:

diff()

  • 写diff()一般都够用。进阶的理解diff(X):对于一个向量来说,diff(X)就是 [X(2)-X(1) X(3)-X(2) … X(n)-X(n-1)] 也就是说求相邻2点的距离。对于一个矩阵来说,结果是:[X(2:n,:) - X(1:n-1,:)];对于一个N*D的矩阵,结果是后一行减前一行的差值。

fillna(0)

  • 空值设置为0

kind=‘line’,

  • 'line’表示图表类型为折线图,缺省为条形图

period_label={‘x’: 0.25, ‘y’: 0.9}#x,y坐标的周期
period_label={‘x’: 参数1, ‘y’: 参数2}#x,y坐标的周期

  • 参数1 x方向周期
  • 参数2 y方向周期

折线图数据集

保存为文件covid19.csv使用,建议通过码云下载

date,Belgium,Brazil,Canada,China,France,Germany,India,Indonesia,Iran,Ireland,Italy,Mexico,Netherlands,Portugal,Spain,Sweden,Switzerland,Turkey,USA,United Kingdom
2020-02-26,,,,2717.0,2.0,,,,19.0,,12.0,,,,,,,,,
2020-02-27,,,,2746.0,2.0,,,,26.0,,17.0,,,,,,,,,
2020-02-28,,,,2790.0,2.0,,,,34.0,,21.0,,,,,,,,,
2020-02-29,,,,2837.0,2.0,,,,43.0,,29.0,,,,,,,,1.0,
2020-03-01,,,,2872.0,2.0,,,,54.0,,34.0,,,,,,,,1.0,
2020-03-02,,,,2914.0,3.0,,,,66.0,,52.0,,,,,,,,6.0,
2020-03-03,,,,2947.0,4.0,,,,77.0,,79.0,,,,1.0,,,,7.0,
2020-03-04,,,,2983.0,4.0,,,,92.0,,107.0,,,,2.0,,,,11.0,
2020-03-05,,,,3015.0,6.0,,,,107.0,,148.0,,,,3.0,,1.0,,12.0,1.0
2020-03-06,,,,3044.0,9.0,,,,124.0,,197.0,,1.0,,5.0,,1.0,,14.0,2.0
2020-03-07,,,,3072.0,11.0,,,,145.0,,233.0,,1.0,,10.0,,1.0,,17.0,2.0
2020-03-08,,,,3100.0,19.0,,,,194.0,,366.0,,3.0,,17.0,,2.0,,21.0,3.0
2020-03-09,,,1.0,3123.0,19.0,2.0,,,237.0,,463.0,,3.0,,28.0,,2.0,,22.0,4.0
2020-03-10,,,1.0,3139.0,33.0,2.0,,,291.0,,631.0,,4.0,,35.0,,3.0,,28.0,6.0
2020-03-11,3.0,,1.0,3161.0,48.0,3.0,1.0,1.0,354.0,1.0,827.0,,5.0,,54.0,1.0,4.0,,32.0,8.0
2020-03-12,3.0,,1.0,3172.0,48.0,3.0,1.0,1.0,429.0,1.0,1000.0,,5.0,,55.0,1.0,4.0,,40.0,8.0
2020-03-13,3.0,,1.0,3180.0,79.0,7.0,2.0,4.0,514.0,1.0,1266.0,,10.0,,133.0,1.0,11.0,,48.0,8.0
2020-03-14,4.0,,1.0,3193.0,91.0,9.0,2.0,5.0,611.0,2.0,1441.0,,12.0,,195.0,2.0,13.0,,54.0,21.0
2020-03-15,4.0,,1.0,3203.0,91.0,11.0,2.0,5.0,724.0,2.0,1809.0,,20.0,,289.0,3.0,14.0,,60.0,21.0
2020-03-16,5.0,,4.0,3217.0,149.0,17.0,2.0,5.0,853.0,2.0,2158.0,,24.0,,342.0,6.0,14.0,,84.0,56.0
2020-03-17,10.0,1.0,5.0,3230.0,149.0,24.0,3.0,5.0,988.0,2.0,2503.0,,43.0,1.0,533.0,7.0,27.0,1.0,107.0,56.0
2020-03-18,14.0,3.0,8.0,3241.0,149.0,28.0,3.0,19.0,1135.0,2.0,2978.0,,58.0,2.0,623.0,10.0,28.0,1.0,143.0,72.0
2020-03-19,21.0,6.0,9.0,3249.0,244.0,44.0,4.0,25.0,1284.0,3.0,3405.0,1.0,77.0,3.0,830.0,11.0,41.0,3.0,209.0,138.0
2020-03-20,37.0,11.0,12.0,3253.0,451.0,67.0,5.0,32.0,1433.0,3.0,4032.0,1.0,107.0,6.0,1043.0,16.0,54.0,4.0,260.0,178.0
2020-03-21,67.0,15.0,19.0,3259.0,563.0,84.0,6.0,38.0,1556.0,3.0,4825.0,2.0,137.0,12.0,1375.0,20.0,75.0,9.0,320.0,234.0
2020-03-22,75.0,25.0,21.0,3274.0,676.0,94.0,7.0,48.0,1685.0,4.0,5476.0,2.0,180.0,14.0,1772.0,21.0,98.0,30.0,427.0,282.0
2020-03-23,88.0,34.0,25.0,3274.0,862.0,123.0,10.0,49.0,1812.0,6.0,6077.0,3.0,214.0,23.0,2311.0,25.0,120.0,37.0,552.0,336.0
2020-03-24,122.0,46.0,26.0,3281.0,1102.0,157.0,10.0,55.0,1934.0,7.0,6820.0,4.0,277.0,33.0,2808.0,36.0,122.0,44.0,706.0,423.0
2020-03-25,178.0,59.0,30.0,3285.0,1333.0,206.0,12.0,58.0,2077.0,9.0,7503.0,5.0,357.0,43.0,3647.0,62.0,153.0,59.0,943.0,466.0
2020-03-26,220.0,77.0,38.0,3291.0,1698.0,267.0,20.0,78.0,2234.0,19.0,8215.0,6.0,435.0,60.0,4365.0,77.0,191.0,75.0,1210.0,580.0
2020-03-27,289.0,92.0,54.0,3296.0,1997.0,342.0,20.0,87.0,2378.0,22.0,9134.0,8.0,547.0,76.0,5138.0,105.0,231.0,92.0,1582.0,761.0
2020-03-28,353.0,111.0,61.0,3299.0,2317.0,433.0,24.0,102.0,2517.0,36.0,10023.0,12.0,640.0,100.0,5982.0,105.0,264.0,108.0,2182.0,1021.0
2020-03-29,431.0,136.0,64.0,3304.0,2611.0,533.0,27.0,114.0,2640.0,46.0,10779.0,16.0,772.0,119.0,6803.0,110.0,300.0,131.0,2566.0,1231.0
2020-03-30,513.0,159.0,80.0,3308.0,3030.0,645.0,32.0,122.0,2757.0,54.0,11591.0,20.0,865.0,140.0,7716.0,146.0,359.0,168.0,3112.0,1411.0
2020-03-31,705.0,201.0,101.0,3309.0,3532.0,775.0,35.0,136.0,2898.0,71.0,12428.0,28.0,1040.0,160.0,8464.0,180.0,433.0,214.0,4039.0,1793.0
2020-04-01,828.0,240.0,109.0,3316.0,4414.0,920.0,58.0,157.0,3036.0,85.0,13155.0,29.0,1175.0,187.0,9387.0,239.0,488.0,277.0,4995.0,2357.0
2020-04-02,1011.0,324.0,139.0,3322.0,5398.0,1107.0,72.0,170.0,3160.0,98.0,13915.0,37.0,1341.0,209.0,10348.0,308.0,536.0,356.0,6294.0,2926.0
2020-04-03,1143.0,359.0,179.0,3326.0,6520.0,1275.0,72.0,181.0,3294.0,120.0,14681.0,50.0,1490.0,246.0,11198.0,358.0,591.0,425.0,7418.0,3611.0
2020-04-04,1283.0,445.0,218.0,3330.0,7574.0,1444.0,86.0,191.0,3452.0,137.0,15362.0,60.0,1656.0,266.0,11947.0,373.0,666.0,501.0,8387.0,4320.0
2020-04-05,1447.0,486.0,259.0,3333.0,8093.0,1584.0,99.0,198.0,3603.0,158.0,15887.0,79.0,1771.0,295.0,12641.0,401.0,715.0,574.0,9489.0,4943.0
2020-04-06,1632.0,564.0,339.0,3335.0,8926.0,1810.0,136.0,209.0,3739.0,174.0,16523.0,94.0,1874.0,311.0,13341.0,477.0,765.0,649.0,10783.0,5385.0
2020-04-07,2035.0,686.0,375.0,3335.0,10343.0,2016.0,150.0,221.0,3872.0,210.0,17127.0,125.0,2108.0,345.0,14045.0,591.0,821.0,725.0,12798.0,6171.0
2020-04-08,2240.0,819.0,407.0,3337.0,10887.0,2349.0,178.0,240.0,3993.0,235.0,17669.0,141.0,2255.0,380.0,14792.0,687.0,895.0,812.0,14704.0,7111.0
2020-04-09,2523.0,950.0,503.0,3339.0,12228.0,2607.0,226.0,280.0,4110.0,263.0,18279.0,174.0,2403.0,409.0,15447.0,793.0,948.0,908.0,16553.0,7993.0
2020-04-10,3019.0,1057.0,557.0,3340.0,13215.0,2767.0,246.0,306.0,4232.0,287.0,18849.0,194.0,2520.0,435.0,16081.0,870.0,1002.0,1006.0,18595.0,8974.0
2020-04-11,3346.0,1124.0,654.0,3343.0,13851.0,2894.0,288.0,327.0,4357.0,320.0,19468.0,233.0,2653.0,470.0,16606.0,887.0,1036.0,1101.0,20471.0,9892.0
2020-04-12,3600.0,1223.0,714.0,3343.0,14412.0,3022.0,331.0,373.0,4474.0,334.0,19899.0,273.0,2747.0,504.0,17209.0,899.0,1106.0,1198.0,22032.0,10629.0
2020-04-13,3903.0,1328.0,779.0,3345.0,14986.0,3194.0,358.0,399.0,4585.0,365.0,20465.0,296.0,2833.0,535.0,17756.0,919.0,1138.0,1296.0,23546.0,11347.0
2020-04-14,4157.0,1532.0,899.0,3345.0,15748.0,3294.0,393.0,459.0,4683.0,406.0,21067.0,332.0,2955.0,567.0,18056.0,1033.0,1174.0,1403.0,25854.0,12129.0
2020-04-15,4440.0,1736.0,1006.0,3346.0,17188.0,3804.0,405.0,469.0,4777.0,444.0,21645.0,406.0,3145.0,599.0,18708.0,1203.0,1239.0,1518.0,28341.0,12894.0
2020-04-16,4857.0,1924.0,1257.0,3346.0,17941.0,4052.0,448.0,496.0,4869.0,486.0,22170.0,449.0,3327.0,629.0,19315.0,1333.0,1281.0,1643.0,32933.0,13759.0
2020-04-17,5163.0,2141.0,1354.0,4636.0,18703.0,4352.0,486.0,520.0,4958.0,530.0,22745.0,486.0,3471.0,657.0,20002.0,1400.0,1327.0,1769.0,36790.0,14607.0
2020-04-18,5453.0,2354.0,1399.0,4636.0,19345.0,4459.0,521.0,535.0,5031.0,571.0,23227.0,546.0,3613.0,687.0,20043.0,1511.0,1368.0,1890.0,38671.0,15498.0
2020-04-19,5683.0,2462.0,1563.0,4636.0,19744.0,4586.0,559.0,582.0,5118.0,610.0,23660.0,650.0,3697.0,714.0,20453.0,1540.0,1393.0,2017.0,40664.0,16095.0
2020-04-20,5828.0,2587.0,1725.0,4636.0,20292.0,4862.0,592.0,590.0,5209.0,687.0,24114.0,686.0,3764.0,735.0,20852.0,1580.0,1429.0,2140.0,42097.0,16550.0
2020-04-21,5998.0,2741.0,1908.0,4636.0,20829.0,5033.0,645.0,616.0,5297.0,730.0,24648.0,712.0,3929.0,762.0,21282.0,1765.0,1478.0,2259.0,44447.0,17378.0
2020-04-22,6262.0,2906.0,2075.0,4636.0,21373.0,5279.0,681.0,635.0,5391.0,769.0,25085.0,857.0,4068.0,785.0,21717.0,1937.0,1509.0,2376.0,46628.0,18151.0

折线图例程

# @Time    : 2022/1/12 19:37
# @Author  : 南黎
# @FileName: 2.折线图.py
import pandas_alive
import pandas as pd
######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

df = pd.read_csv("数据源data/covid19.csv", index_col=0, parse_dates=[0], thousands=',')

# diff() 你就写diff()一般都够用。进阶的理解diff(X):对于一个向量来说,diff(X)就是 [X(2)-X(1) X(3)-X(2) … X(n)-X(n-1)] 也就是说求相邻2点的距离。对于一个矩阵来说,结果是:[X(2:n,:) - X(1:n-1,:)];对于一个N*D的矩阵,结果是后一行减前一行的差值。
# fillna(0) 空值设置为0
# period_label={'x':0.25,'y':0.9} 表示

df.diff().fillna(0).plot_animated(
    filename='2.折线图.gif',
    title='发现你走远了——2.折线图',
    kind='line',#图表类型为折线图
    period_label={'x': 0.25, 'y': 0.9}#x,y坐标的周期
)

图片为了能上传经过压缩,你生成的效果应该更好一点!


3.散点图

会出现提醒:

E:\allworkspace\python work space\Python动态图_pandas_alive\pandas_alive\charts.py:420: UserWarning: Discarding nonzero nanoseconds in conversion
  super().set_x_y_limits(self.df, i, self.ax)

在这里插入图片描述
意思是用户警告:在转换 super().set_x_y_limits(self.df, i, self.ax) 中丢弃非零纳秒,我理解应该是精度的丢失,不影响出图的。

散点图API说明:

parse_dates={“Timestamp”: [“Year”, “Month”, “Day”]}

  • 解析数据为年-月-日格式

dt.strftime(’%Y/%m/%d’)

  • 标准化这一列数据格式输出 年-月-日

kind=‘scatter’,

  • kind=“scatter”,图表类型为散点图

keep_columns = [“Minimum temperature (Degree C)”, “Maximum temperature (Degree C)”]

  • 设置图例内容

散点图数据集

Newcastle_Australia_Max_Temps.csv
Newcastle_Australia_Min_Temps.csv
数据量过大,移步码云下载数据集

https://gitee.com/miao-zehao/pandas-live-game-graph

【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第17张图片

散点图例程

# @Time    : 2022/1/12 19:57
# @Author  : 南黎
# @FileName: 3..py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas as pd
import pandas_alive

max_temp_df = pd.read_csv(
    "数据源data/Newcastle_Australia_Max_Temps.csv",
    parse_dates={"Timestamp": ["Year", "Month", "Day"]},
)
min_temp_df = pd.read_csv(
    "数据源data/Newcastle_Australia_Min_Temps.csv",
    parse_dates={"Timestamp": ["Year", "Month", "Day"]},
)

merged_temp_df = pd.merge_asof(max_temp_df, min_temp_df, on="Timestamp")

merged_temp_df.index = pd.to_datetime(merged_temp_df["Timestamp"].dt.strftime('%Y/%m/%d'))

#设置图例内容
keep_columns = ["Minimum temperature (Degree C)", "Maximum temperature (Degree C)"]

merged_temp_df[keep_columns].resample("Y").mean().plot_animated(
    filename='3.散点图.gif',
    kind="scatter",#图表类型为散点图
    title='发现你走远了——3.散点图'
)

在这里插入图片描述


4.饼状图

饼状图API说明:

kind=‘pie’,

  • kind=“pie”,图表类型为饼状图

rotatelabels=True

  • 是否可以旋转,True表示可以旋转

饼状图数据集

保存为文件covid19.csv使用,建议通过码云下载

date,Belgium,Brazil,Canada,China,France,Germany,India,Indonesia,Iran,Ireland,Italy,Mexico,Netherlands,Portugal,Spain,Sweden,Switzerland,Turkey,USA,United Kingdom
2020-02-26,,,,2717.0,2.0,,,,19.0,,12.0,,,,,,,,,
2020-02-27,,,,2746.0,2.0,,,,26.0,,17.0,,,,,,,,,
2020-02-28,,,,2790.0,2.0,,,,34.0,,21.0,,,,,,,,,
2020-02-29,,,,2837.0,2.0,,,,43.0,,29.0,,,,,,,,1.0,
2020-03-01,,,,2872.0,2.0,,,,54.0,,34.0,,,,,,,,1.0,
2020-03-02,,,,2914.0,3.0,,,,66.0,,52.0,,,,,,,,6.0,
2020-03-03,,,,2947.0,4.0,,,,77.0,,79.0,,,,1.0,,,,7.0,
2020-03-04,,,,2983.0,4.0,,,,92.0,,107.0,,,,2.0,,,,11.0,
2020-03-05,,,,3015.0,6.0,,,,107.0,,148.0,,,,3.0,,1.0,,12.0,1.0
2020-03-06,,,,3044.0,9.0,,,,124.0,,197.0,,1.0,,5.0,,1.0,,14.0,2.0
2020-03-07,,,,3072.0,11.0,,,,145.0,,233.0,,1.0,,10.0,,1.0,,17.0,2.0
2020-03-08,,,,3100.0,19.0,,,,194.0,,366.0,,3.0,,17.0,,2.0,,21.0,3.0
2020-03-09,,,1.0,3123.0,19.0,2.0,,,237.0,,463.0,,3.0,,28.0,,2.0,,22.0,4.0
2020-03-10,,,1.0,3139.0,33.0,2.0,,,291.0,,631.0,,4.0,,35.0,,3.0,,28.0,6.0
2020-03-11,3.0,,1.0,3161.0,48.0,3.0,1.0,1.0,354.0,1.0,827.0,,5.0,,54.0,1.0,4.0,,32.0,8.0
2020-03-12,3.0,,1.0,3172.0,48.0,3.0,1.0,1.0,429.0,1.0,1000.0,,5.0,,55.0,1.0,4.0,,40.0,8.0
2020-03-13,3.0,,1.0,3180.0,79.0,7.0,2.0,4.0,514.0,1.0,1266.0,,10.0,,133.0,1.0,11.0,,48.0,8.0
2020-03-14,4.0,,1.0,3193.0,91.0,9.0,2.0,5.0,611.0,2.0,1441.0,,12.0,,195.0,2.0,13.0,,54.0,21.0
2020-03-15,4.0,,1.0,3203.0,91.0,11.0,2.0,5.0,724.0,2.0,1809.0,,20.0,,289.0,3.0,14.0,,60.0,21.0
2020-03-16,5.0,,4.0,3217.0,149.0,17.0,2.0,5.0,853.0,2.0,2158.0,,24.0,,342.0,6.0,14.0,,84.0,56.0
2020-03-17,10.0,1.0,5.0,3230.0,149.0,24.0,3.0,5.0,988.0,2.0,2503.0,,43.0,1.0,533.0,7.0,27.0,1.0,107.0,56.0
2020-03-18,14.0,3.0,8.0,3241.0,149.0,28.0,3.0,19.0,1135.0,2.0,2978.0,,58.0,2.0,623.0,10.0,28.0,1.0,143.0,72.0
2020-03-19,21.0,6.0,9.0,3249.0,244.0,44.0,4.0,25.0,1284.0,3.0,3405.0,1.0,77.0,3.0,830.0,11.0,41.0,3.0,209.0,138.0
2020-03-20,37.0,11.0,12.0,3253.0,451.0,67.0,5.0,32.0,1433.0,3.0,4032.0,1.0,107.0,6.0,1043.0,16.0,54.0,4.0,260.0,178.0
2020-03-21,67.0,15.0,19.0,3259.0,563.0,84.0,6.0,38.0,1556.0,3.0,4825.0,2.0,137.0,12.0,1375.0,20.0,75.0,9.0,320.0,234.0
2020-03-22,75.0,25.0,21.0,3274.0,676.0,94.0,7.0,48.0,1685.0,4.0,5476.0,2.0,180.0,14.0,1772.0,21.0,98.0,30.0,427.0,282.0
2020-03-23,88.0,34.0,25.0,3274.0,862.0,123.0,10.0,49.0,1812.0,6.0,6077.0,3.0,214.0,23.0,2311.0,25.0,120.0,37.0,552.0,336.0
2020-03-24,122.0,46.0,26.0,3281.0,1102.0,157.0,10.0,55.0,1934.0,7.0,6820.0,4.0,277.0,33.0,2808.0,36.0,122.0,44.0,706.0,423.0
2020-03-25,178.0,59.0,30.0,3285.0,1333.0,206.0,12.0,58.0,2077.0,9.0,7503.0,5.0,357.0,43.0,3647.0,62.0,153.0,59.0,943.0,466.0
2020-03-26,220.0,77.0,38.0,3291.0,1698.0,267.0,20.0,78.0,2234.0,19.0,8215.0,6.0,435.0,60.0,4365.0,77.0,191.0,75.0,1210.0,580.0
2020-03-27,289.0,92.0,54.0,3296.0,1997.0,342.0,20.0,87.0,2378.0,22.0,9134.0,8.0,547.0,76.0,5138.0,105.0,231.0,92.0,1582.0,761.0
2020-03-28,353.0,111.0,61.0,3299.0,2317.0,433.0,24.0,102.0,2517.0,36.0,10023.0,12.0,640.0,100.0,5982.0,105.0,264.0,108.0,2182.0,1021.0
2020-03-29,431.0,136.0,64.0,3304.0,2611.0,533.0,27.0,114.0,2640.0,46.0,10779.0,16.0,772.0,119.0,6803.0,110.0,300.0,131.0,2566.0,1231.0
2020-03-30,513.0,159.0,80.0,3308.0,3030.0,645.0,32.0,122.0,2757.0,54.0,11591.0,20.0,865.0,140.0,7716.0,146.0,359.0,168.0,3112.0,1411.0
2020-03-31,705.0,201.0,101.0,3309.0,3532.0,775.0,35.0,136.0,2898.0,71.0,12428.0,28.0,1040.0,160.0,8464.0,180.0,433.0,214.0,4039.0,1793.0
2020-04-01,828.0,240.0,109.0,3316.0,4414.0,920.0,58.0,157.0,3036.0,85.0,13155.0,29.0,1175.0,187.0,9387.0,239.0,488.0,277.0,4995.0,2357.0
2020-04-02,1011.0,324.0,139.0,3322.0,5398.0,1107.0,72.0,170.0,3160.0,98.0,13915.0,37.0,1341.0,209.0,10348.0,308.0,536.0,356.0,6294.0,2926.0
2020-04-03,1143.0,359.0,179.0,3326.0,6520.0,1275.0,72.0,181.0,3294.0,120.0,14681.0,50.0,1490.0,246.0,11198.0,358.0,591.0,425.0,7418.0,3611.0
2020-04-04,1283.0,445.0,218.0,3330.0,7574.0,1444.0,86.0,191.0,3452.0,137.0,15362.0,60.0,1656.0,266.0,11947.0,373.0,666.0,501.0,8387.0,4320.0
2020-04-05,1447.0,486.0,259.0,3333.0,8093.0,1584.0,99.0,198.0,3603.0,158.0,15887.0,79.0,1771.0,295.0,12641.0,401.0,715.0,574.0,9489.0,4943.0
2020-04-06,1632.0,564.0,339.0,3335.0,8926.0,1810.0,136.0,209.0,3739.0,174.0,16523.0,94.0,1874.0,311.0,13341.0,477.0,765.0,649.0,10783.0,5385.0
2020-04-07,2035.0,686.0,375.0,3335.0,10343.0,2016.0,150.0,221.0,3872.0,210.0,17127.0,125.0,2108.0,345.0,14045.0,591.0,821.0,725.0,12798.0,6171.0
2020-04-08,2240.0,819.0,407.0,3337.0,10887.0,2349.0,178.0,240.0,3993.0,235.0,17669.0,141.0,2255.0,380.0,14792.0,687.0,895.0,812.0,14704.0,7111.0
2020-04-09,2523.0,950.0,503.0,3339.0,12228.0,2607.0,226.0,280.0,4110.0,263.0,18279.0,174.0,2403.0,409.0,15447.0,793.0,948.0,908.0,16553.0,7993.0
2020-04-10,3019.0,1057.0,557.0,3340.0,13215.0,2767.0,246.0,306.0,4232.0,287.0,18849.0,194.0,2520.0,435.0,16081.0,870.0,1002.0,1006.0,18595.0,8974.0
2020-04-11,3346.0,1124.0,654.0,3343.0,13851.0,2894.0,288.0,327.0,4357.0,320.0,19468.0,233.0,2653.0,470.0,16606.0,887.0,1036.0,1101.0,20471.0,9892.0
2020-04-12,3600.0,1223.0,714.0,3343.0,14412.0,3022.0,331.0,373.0,4474.0,334.0,19899.0,273.0,2747.0,504.0,17209.0,899.0,1106.0,1198.0,22032.0,10629.0
2020-04-13,3903.0,1328.0,779.0,3345.0,14986.0,3194.0,358.0,399.0,4585.0,365.0,20465.0,296.0,2833.0,535.0,17756.0,919.0,1138.0,1296.0,23546.0,11347.0
2020-04-14,4157.0,1532.0,899.0,3345.0,15748.0,3294.0,393.0,459.0,4683.0,406.0,21067.0,332.0,2955.0,567.0,18056.0,1033.0,1174.0,1403.0,25854.0,12129.0
2020-04-15,4440.0,1736.0,1006.0,3346.0,17188.0,3804.0,405.0,469.0,4777.0,444.0,21645.0,406.0,3145.0,599.0,18708.0,1203.0,1239.0,1518.0,28341.0,12894.0
2020-04-16,4857.0,1924.0,1257.0,3346.0,17941.0,4052.0,448.0,496.0,4869.0,486.0,22170.0,449.0,3327.0,629.0,19315.0,1333.0,1281.0,1643.0,32933.0,13759.0
2020-04-17,5163.0,2141.0,1354.0,4636.0,18703.0,4352.0,486.0,520.0,4958.0,530.0,22745.0,486.0,3471.0,657.0,20002.0,1400.0,1327.0,1769.0,36790.0,14607.0
2020-04-18,5453.0,2354.0,1399.0,4636.0,19345.0,4459.0,521.0,535.0,5031.0,571.0,23227.0,546.0,3613.0,687.0,20043.0,1511.0,1368.0,1890.0,38671.0,15498.0
2020-04-19,5683.0,2462.0,1563.0,4636.0,19744.0,4586.0,559.0,582.0,5118.0,610.0,23660.0,650.0,3697.0,714.0,20453.0,1540.0,1393.0,2017.0,40664.0,16095.0
2020-04-20,5828.0,2587.0,1725.0,4636.0,20292.0,4862.0,592.0,590.0,5209.0,687.0,24114.0,686.0,3764.0,735.0,20852.0,1580.0,1429.0,2140.0,42097.0,16550.0
2020-04-21,5998.0,2741.0,1908.0,4636.0,20829.0,5033.0,645.0,616.0,5297.0,730.0,24648.0,712.0,3929.0,762.0,21282.0,1765.0,1478.0,2259.0,44447.0,17378.0
2020-04-22,6262.0,2906.0,2075.0,4636.0,21373.0,5279.0,681.0,635.0,5391.0,769.0,25085.0,857.0,4068.0,785.0,21717.0,1937.0,1509.0,2376.0,46628.0,18151.0

饼状图例程

# @Time    : 2022/1/13 5:57
# @Author  : 南黎
# @FileName: 4.饼状图.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################
import pandas_alive

df = pd.read_csv("数据源data/covid19.csv", index_col=0, parse_dates=[0], thousands=',')

df.plot_animated(filename='4.饼状图.gif',
                 title='发现你走远了——4.饼状图',
                 kind="pie",#图标类型为饼状图
                 rotatelabels=True,#是否可以旋转
                 period_label={'x': 0, 'y': 0})#坐标轴周期


5.气泡图

有一个运动的气泡

气泡图API说明:

暂无

气泡图数据集

码云下载multi.csv

气泡图例程

# @Time    : 2022/1/12 19:58
# @Author  : 南黎
# @FileName: 5.气泡图.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas_alive

multi_index_df = pd.read_csv("数据源data/multi.csv", header=[0, 1], index_col=0)

multi_index_df.index = pd.to_datetime(multi_index_df.index, dayfirst=True)

map_chart = multi_index_df.plot_animated(
    kind="bubble",
    filename="5.气泡图.gif",
    title='发现你走远了——5.气泡图',
    x_data_label="Longitude",  # x数据标签 使用数据集的Longitude属性
    y_data_label="Latitude",  # y数据标签 使用数据集的Latitude属性
    size_data_label="Cases",
    color_data_label="Cases",
    vmax=5,
    steps_per_period=3,#每个周期的步数
    interpolate_period=True, period_length=500,
    dpi=100
)


6.地理空间图

地理空间点图和多边形地理空间图,要用gpkg格式的数据集。一般人用不到,回头我再学习完善一下说明。

地理空间图表现在可以使用[geopandas]轻松设置动画(https://geopandas.org/index.html)!

如果使用Windows,anaconda是安装所有GDAL依赖项的最简单方法。

必须以包含“宽”数据的“geopandas”GeoDataFrame开始,其中:

-每行表示一个几何体(点或多边形)。

-索引包含几何图形标签(可选)

-每列表示一个时间段。

通过使用“df=df”将与其他图表兼容的数据进行转换,可以很容易地组合这些图表。T`。

我发现了项目的bug
【用pandas_alive几行代码绘制竞赛动图】全网首发pandas_alive数据可视化中文学习笔记合集,学不会来打我(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)_第18张图片
没有标题的·····

6.1 地理空间点图

地理空间点图API说明:

暂无

地理空间点图数据集

码云下载nsw-covid19-cases-by-postcode.gpkg

地理空间点图例程

# @Time    : 2022/1/14 19:58
# @Author  : 南黎
# @FileName: 6.1地理空间点图.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import geopandas
import pandas_alive
import contextily

gdf = geopandas.read_file('数据源data/nsw-covid19-cases-by-postcode.gpkg')
gdf.index = gdf.postcode
gdf = gdf.drop('postcode', axis=1)

map_chart = gdf.plot_animated(
    filename='6.1地理空间点图.gif',
    title='发现你走远了——6.1地理空间点图',
    enable_progress_bar=True,
    basemap_format={'source': contextily.providers.Stamen.Terrain}
)


6.2 多边形地理空间图

支持包含多边形的GeoDataFrames!

多边形地理空间图API说明:

暂无

多边形地理空间图数据集

码云下载italy-covid-region.gpkg

多边形地理空间图例程

# @Time    : 2022/1/14 21:04
# @Author  : 南黎
# @FileName: 6.2多边形地理空间图.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import geopandas
import pandas_alive
import contextily

gdf = geopandas.read_file('数据源data/italy-covid-region.gpkg')
gdf.index = gdf.region
gdf = gdf.drop('region', axis=1)

map_chart = gdf.plot_animated(
    filename='6.2多边形地理空间图.gif',
    title='发现你走远了——6.2多边形地理空间图',
    enable_progress_bar=True,
    basemap_format={'source': contextily.providers.Stamen.Terrain}
)


7.多个图表

在一张gif图中显示多张表。

多个图表API说明:

plots=[animated_bar_chart, animated_line_chart],

  • 两个表放进一个列表中,参数plots放入的是要被绘制进主图的两张子图
    enable_progress_bar=True
  • 是否在生成图片时显示生成图片的进度,以一个进度条的方式呈现
    在这里插入图片描述

关于如何自定义多张表之间的距离,标题等功能,在下一节中具体说明

【8.城市人口(测试代码+数据集+绘图参数解析)】

多个图表数据集

保存文件名为 covid19.csv,建议码云下载,否则可能出现编码问题

date,Belgium,Brazil,Canada,China,France,Germany,India,Indonesia,Iran,Ireland,Italy,Mexico,Netherlands,Portugal,Spain,Sweden,Switzerland,Turkey,USA,United Kingdom
2020-02-26,,,,2717.0,2.0,,,,19.0,,12.0,,,,,,,,,
2020-02-27,,,,2746.0,2.0,,,,26.0,,17.0,,,,,,,,,
2020-02-28,,,,2790.0,2.0,,,,34.0,,21.0,,,,,,,,,
2020-02-29,,,,2837.0,2.0,,,,43.0,,29.0,,,,,,,,1.0,
2020-03-01,,,,2872.0,2.0,,,,54.0,,34.0,,,,,,,,1.0,
2020-03-02,,,,2914.0,3.0,,,,66.0,,52.0,,,,,,,,6.0,
2020-03-03,,,,2947.0,4.0,,,,77.0,,79.0,,,,1.0,,,,7.0,
2020-03-04,,,,2983.0,4.0,,,,92.0,,107.0,,,,2.0,,,,11.0,
2020-03-05,,,,3015.0,6.0,,,,107.0,,148.0,,,,3.0,,1.0,,12.0,1.0
2020-03-06,,,,3044.0,9.0,,,,124.0,,197.0,,1.0,,5.0,,1.0,,14.0,2.0
2020-03-07,,,,3072.0,11.0,,,,145.0,,233.0,,1.0,,10.0,,1.0,,17.0,2.0
2020-03-08,,,,3100.0,19.0,,,,194.0,,366.0,,3.0,,17.0,,2.0,,21.0,3.0
2020-03-09,,,1.0,3123.0,19.0,2.0,,,237.0,,463.0,,3.0,,28.0,,2.0,,22.0,4.0
2020-03-10,,,1.0,3139.0,33.0,2.0,,,291.0,,631.0,,4.0,,35.0,,3.0,,28.0,6.0
2020-03-11,3.0,,1.0,3161.0,48.0,3.0,1.0,1.0,354.0,1.0,827.0,,5.0,,54.0,1.0,4.0,,32.0,8.0
2020-03-12,3.0,,1.0,3172.0,48.0,3.0,1.0,1.0,429.0,1.0,1000.0,,5.0,,55.0,1.0,4.0,,40.0,8.0
2020-03-13,3.0,,1.0,3180.0,79.0,7.0,2.0,4.0,514.0,1.0,1266.0,,10.0,,133.0,1.0,11.0,,48.0,8.0
2020-03-14,4.0,,1.0,3193.0,91.0,9.0,2.0,5.0,611.0,2.0,1441.0,,12.0,,195.0,2.0,13.0,,54.0,21.0
2020-03-15,4.0,,1.0,3203.0,91.0,11.0,2.0,5.0,724.0,2.0,1809.0,,20.0,,289.0,3.0,14.0,,60.0,21.0
2020-03-16,5.0,,4.0,3217.0,149.0,17.0,2.0,5.0,853.0,2.0,2158.0,,24.0,,342.0,6.0,14.0,,84.0,56.0
2020-03-17,10.0,1.0,5.0,3230.0,149.0,24.0,3.0,5.0,988.0,2.0,2503.0,,43.0,1.0,533.0,7.0,27.0,1.0,107.0,56.0
2020-03-18,14.0,3.0,8.0,3241.0,149.0,28.0,3.0,19.0,1135.0,2.0,2978.0,,58.0,2.0,623.0,10.0,28.0,1.0,143.0,72.0
2020-03-19,21.0,6.0,9.0,3249.0,244.0,44.0,4.0,25.0,1284.0,3.0,3405.0,1.0,77.0,3.0,830.0,11.0,41.0,3.0,209.0,138.0
2020-03-20,37.0,11.0,12.0,3253.0,451.0,67.0,5.0,32.0,1433.0,3.0,4032.0,1.0,107.0,6.0,1043.0,16.0,54.0,4.0,260.0,178.0
2020-03-21,67.0,15.0,19.0,3259.0,563.0,84.0,6.0,38.0,1556.0,3.0,4825.0,2.0,137.0,12.0,1375.0,20.0,75.0,9.0,320.0,234.0
2020-03-22,75.0,25.0,21.0,3274.0,676.0,94.0,7.0,48.0,1685.0,4.0,5476.0,2.0,180.0,14.0,1772.0,21.0,98.0,30.0,427.0,282.0
2020-03-23,88.0,34.0,25.0,3274.0,862.0,123.0,10.0,49.0,1812.0,6.0,6077.0,3.0,214.0,23.0,2311.0,25.0,120.0,37.0,552.0,336.0
2020-03-24,122.0,46.0,26.0,3281.0,1102.0,157.0,10.0,55.0,1934.0,7.0,6820.0,4.0,277.0,33.0,2808.0,36.0,122.0,44.0,706.0,423.0
2020-03-25,178.0,59.0,30.0,3285.0,1333.0,206.0,12.0,58.0,2077.0,9.0,7503.0,5.0,357.0,43.0,3647.0,62.0,153.0,59.0,943.0,466.0
2020-03-26,220.0,77.0,38.0,3291.0,1698.0,267.0,20.0,78.0,2234.0,19.0,8215.0,6.0,435.0,60.0,4365.0,77.0,191.0,75.0,1210.0,580.0
2020-03-27,289.0,92.0,54.0,3296.0,1997.0,342.0,20.0,87.0,2378.0,22.0,9134.0,8.0,547.0,76.0,5138.0,105.0,231.0,92.0,1582.0,761.0
2020-03-28,353.0,111.0,61.0,3299.0,2317.0,433.0,24.0,102.0,2517.0,36.0,10023.0,12.0,640.0,100.0,5982.0,105.0,264.0,108.0,2182.0,1021.0
2020-03-29,431.0,136.0,64.0,3304.0,2611.0,533.0,27.0,114.0,2640.0,46.0,10779.0,16.0,772.0,119.0,6803.0,110.0,300.0,131.0,2566.0,1231.0
2020-03-30,513.0,159.0,80.0,3308.0,3030.0,645.0,32.0,122.0,2757.0,54.0,11591.0,20.0,865.0,140.0,7716.0,146.0,359.0,168.0,3112.0,1411.0
2020-03-31,705.0,201.0,101.0,3309.0,3532.0,775.0,35.0,136.0,2898.0,71.0,12428.0,28.0,1040.0,160.0,8464.0,180.0,433.0,214.0,4039.0,1793.0
2020-04-01,828.0,240.0,109.0,3316.0,4414.0,920.0,58.0,157.0,3036.0,85.0,13155.0,29.0,1175.0,187.0,9387.0,239.0,488.0,277.0,4995.0,2357.0
2020-04-02,1011.0,324.0,139.0,3322.0,5398.0,1107.0,72.0,170.0,3160.0,98.0,13915.0,37.0,1341.0,209.0,10348.0,308.0,536.0,356.0,6294.0,2926.0
2020-04-03,1143.0,359.0,179.0,3326.0,6520.0,1275.0,72.0,181.0,3294.0,120.0,14681.0,50.0,1490.0,246.0,11198.0,358.0,591.0,425.0,7418.0,3611.0
2020-04-04,1283.0,445.0,218.0,3330.0,7574.0,1444.0,86.0,191.0,3452.0,137.0,15362.0,60.0,1656.0,266.0,11947.0,373.0,666.0,501.0,8387.0,4320.0
2020-04-05,1447.0,486.0,259.0,3333.0,8093.0,1584.0,99.0,198.0,3603.0,158.0,15887.0,79.0,1771.0,295.0,12641.0,401.0,715.0,574.0,9489.0,4943.0
2020-04-06,1632.0,564.0,339.0,3335.0,8926.0,1810.0,136.0,209.0,3739.0,174.0,16523.0,94.0,1874.0,311.0,13341.0,477.0,765.0,649.0,10783.0,5385.0
2020-04-07,2035.0,686.0,375.0,3335.0,10343.0,2016.0,150.0,221.0,3872.0,210.0,17127.0,125.0,2108.0,345.0,14045.0,591.0,821.0,725.0,12798.0,6171.0
2020-04-08,2240.0,819.0,407.0,3337.0,10887.0,2349.0,178.0,240.0,3993.0,235.0,17669.0,141.0,2255.0,380.0,14792.0,687.0,895.0,812.0,14704.0,7111.0
2020-04-09,2523.0,950.0,503.0,3339.0,12228.0,2607.0,226.0,280.0,4110.0,263.0,18279.0,174.0,2403.0,409.0,15447.0,793.0,948.0,908.0,16553.0,7993.0
2020-04-10,3019.0,1057.0,557.0,3340.0,13215.0,2767.0,246.0,306.0,4232.0,287.0,18849.0,194.0,2520.0,435.0,16081.0,870.0,1002.0,1006.0,18595.0,8974.0
2020-04-11,3346.0,1124.0,654.0,3343.0,13851.0,2894.0,288.0,327.0,4357.0,320.0,19468.0,233.0,2653.0,470.0,16606.0,887.0,1036.0,1101.0,20471.0,9892.0
2020-04-12,3600.0,1223.0,714.0,3343.0,14412.0,3022.0,331.0,373.0,4474.0,334.0,19899.0,273.0,2747.0,504.0,17209.0,899.0,1106.0,1198.0,22032.0,10629.0
2020-04-13,3903.0,1328.0,779.0,3345.0,14986.0,3194.0,358.0,399.0,4585.0,365.0,20465.0,296.0,2833.0,535.0,17756.0,919.0,1138.0,1296.0,23546.0,11347.0
2020-04-14,4157.0,1532.0,899.0,3345.0,15748.0,3294.0,393.0,459.0,4683.0,406.0,21067.0,332.0,2955.0,567.0,18056.0,1033.0,1174.0,1403.0,25854.0,12129.0
2020-04-15,4440.0,1736.0,1006.0,3346.0,17188.0,3804.0,405.0,469.0,4777.0,444.0,21645.0,406.0,3145.0,599.0,18708.0,1203.0,1239.0,1518.0,28341.0,12894.0
2020-04-16,4857.0,1924.0,1257.0,3346.0,17941.0,4052.0,448.0,496.0,4869.0,486.0,22170.0,449.0,3327.0,629.0,19315.0,1333.0,1281.0,1643.0,32933.0,13759.0
2020-04-17,5163.0,2141.0,1354.0,4636.0,18703.0,4352.0,486.0,520.0,4958.0,530.0,22745.0,486.0,3471.0,657.0,20002.0,1400.0,1327.0,1769.0,36790.0,14607.0
2020-04-18,5453.0,2354.0,1399.0,4636.0,19345.0,4459.0,521.0,535.0,5031.0,571.0,23227.0,546.0,3613.0,687.0,20043.0,1511.0,1368.0,1890.0,38671.0,15498.0
2020-04-19,5683.0,2462.0,1563.0,4636.0,19744.0,4586.0,559.0,582.0,5118.0,610.0,23660.0,650.0,3697.0,714.0,20453.0,1540.0,1393.0,2017.0,40664.0,16095.0
2020-04-20,5828.0,2587.0,1725.0,4636.0,20292.0,4862.0,592.0,590.0,5209.0,687.0,24114.0,686.0,3764.0,735.0,20852.0,1580.0,1429.0,2140.0,42097.0,16550.0
2020-04-21,5998.0,2741.0,1908.0,4636.0,20829.0,5033.0,645.0,616.0,5297.0,730.0,24648.0,712.0,3929.0,762.0,21282.0,1765.0,1478.0,2259.0,44447.0,17378.0
2020-04-22,6262.0,2906.0,2075.0,4636.0,21373.0,5279.0,681.0,635.0,5391.0,769.0,25085.0,857.0,4068.0,785.0,21717.0,1937.0,1509.0,2376.0,46628.0,18151.0

多个图表例程

# @Time    : 2022/1/12 22:13
# @Author  : 南黎
# @FileName: 7.多个图表.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas_alive

df = pd.read_csv("数据源data/covid19.csv", index_col=0, parse_dates=[0], thousands=',')

# 生成第一个表
animated_line_chart = df.diff().fillna(0).plot_animated(
    title='发现你走远了——子图1',
    kind='line',
    period_label=False,
    add_legend=False
)
# 生成第二个表
animated_bar_chart = df.plot_animated(
    title='发现你走远了——子图2',
    n_visible=10
)

# 最后保存图片的时候,用一个列表同时装入2个表
pandas_alive.animate_multiple_plots(
    filename='7.多个图表.gif',
    plots=[animated_bar_chart, animated_line_chart],  # 两个表放进一个列表中
    title='发现你走远了——7.多个图表',
    enable_progress_bar=True  # 是否在生成图片时显示生成图片的进度,以一个进度条的方式呈现
)


8.城市人口

讲述更多关于两张图距离的控制

城市人口API说明:

有部分在代码注释中

n_visible=10,

  • 属性参数取10个

adjust_subplot_left=0.15,

  • float类型,(0,1)之间
  • 设置子图左浮动=0.15,

adjust_subplot_right=0.9,

  • float类型,(0,1)之间
  • 设置子图右浮动=0.9,

adjust_subplot_bottom=0.1,

  • float类型,(0,1)之间
  • 设置子图底部浮动=0.1,

adjust_subplot_top=0.9,

  • float类型,(0,1)之间
  • 设置子图顶部浮动=0.9,

adjust_subplot_wspace=0.2,

  • float类型,(0,1)之间
  • 设置子图间距浮动=0.2,

adjust_subplot_hspace=0.25

  • float类型,(0,1)之间
  • 设置子图空间浮动=0.25,

城市人口数据集

保存文件名为 urban_pop.csv,建议码云下载,否则可能出现编码问题

year,United States,India,China,Ethiopia,Poland,Malaysia,Peru,Venezuela,Iraq,Saudi Arabia,Canada,Algeria,Ukraine,Vietnam,Thailand,"Congo, Dem. Rep.",Spain,South Africa,Colombia,Argentina,Egypt,South Korea,Italy,Philippines,France,United Kingdom,Bangladesh,Iran,Turkey,Germany,Pakistan,Nigeria,Mexico,Russia,Japan,Indonesia,Brazil
1976,160611122,138219074,162497601,3194879,19215135,4802814,9834645,10382196,7530612,4673719,17705309,6919237,28966085,9399461,10568872,6141742,25379947,12445371,15011687,21370520,17210387,17823527,36687131,15245564,39578989,43757644,7667521,15742290,16914583,56885943,18365687,13177979,38883279,90810675,85642808,26596457,67790415
1977,162256678,143699557,165293316,3300643,19625330,5038232,10197056,10778965,7901451,5041250,17923214,7196331,29452016,9652431,11092595,6381644,25880544,12771480,15524763,21797026,17668393,18743336,36955192,15826569,39798460,43833733,8543815,16521005,17474315,56801863,19175026,13868037,40371908,92479950,86538157,28001978,70478354
1978,164005080,149379782,171153535,3406129,20007316,5284698,10567468,11185526,8253727,5441027,18112108,7565661,29928920,9911702,11628473,6630163,26368356,13105135,16052408,22227673,18089504,19679806,37198851,16426578,40001620,43925435,9528585,17336704,18048189,56796181,20032626,14598364,41879098,94157479,87391419,29468690,73245834
1979,165847531,155285824,180399661,3522584,20341874,5539880,10945723,11600322,8598800,5885282,18301934,7956090,30386569,10181044,12176748,6882719,26838008,13454231,16593237,22668000,18530889,20636698,37418639,17048162,40205371,44054299,10622254,18227951,18640066,56865826,20941767,15355488,43407693,95659612,88197927,30996679,76091693
1980,167551171,161444128,189947471,3658252,20663601,5801267,11331194,12022351,8945814,6382806,18549289,8369497,30825447,10447627,12692095,7135884,27289444,13828615,17145845,23122595,18994489,21623805,37607540,17738316,40423470,44195960,11827261,19206467,19252680,57028530,21906732,16131172,44952217,96960865,88958689,32591870,79015954
1981,169552427,167521704,199949784,3817958,20985955,6050366,11723239,12450233,9294818,6937865,18791654,8805955,31287683,10720898,13071774,7389309,27669147,14253248,17710021,23609667,19480106,22617087,37764025,18687449,40659231,44271630,12920085,20274421,20329814,57229147,22897114,17103116,46412878,98228786,89733659,34436558,82013626
1982,171528659,173152676,210823843,4000296,21314754,6305685,12089571,12875246,9646517,7546375,19057229,9265778,31693551,11006067,13450801,7645260,27925417,14700152,18286697,24118025,19987197,23619800,37819718,19680770,40911627,44228171,13607076,21426582,21630562,57267174,23877291,18109529,47890690,99464693,90474900,36463770,85085296
1983,173459636,178956141,220472140,4203323,21651242,6571674,12460705,13303646,9998790,8194651,19287381,9746745,32110163,11301138,13832449,7907632,28151118,15162766,18874699,24636888,20516521,24621925,37822134,20717792,41177471,44215089,14326358,22654500,22977503,57114118,24901258,19156448,49383429,100677781,91224787,38577957,88209014
1984,175321738,184906540,230206255,4422527,21984900,6854613,12837778,13741349,10349519,8862080,19511294,10246073,32533314,11603770,14220724,8182628,28357570,15630401,19472129,25163825,21068787,25577374,37819273,21799606,41450438,44257473,15083858,23944608,24362121,56792461,25962081,20257934,50884710,102058393,91937389,40774954,91357900
1985,177239105,190975975,240414890,4655890,22299554,7158816,13220933,14189017,10697257,9530555,19731489,10759943,32933625,11912008,14616958,8520035,28549379,16134214,20077488,25695319,21643302,26473134,37818886,22923393,41726184,44329755,15880101,25281030,25769767,56485448,27052677,21421320,52390613,103466989,92632808,43042272,94505178
1986,179244877,197165614,251325056,4901173,22589785,7487770,13610395,14647328,11040738,10193888,19967235,11287827,33344880,12225169,15025684,8876843,28723937,16716155,20656600,26230730,22241759,27480866,37809627,24093004,42005647,44404206,16718788,26666054,27034322,56438249,28172326,22654337,53899557,104825013,93348378,45381922,97647144
1987,181215212,203469688,262976051,5160078,22855463,7839689,14005309,15114933,11382713,10774882,20237468,11821049,33766182,12541644,15443627,9253676,28887112,17299431,21228791,26769637,22812398,28537079,37802166,25307752,42289365,44470325,17598710,28011702,28238882,56695484,29321589,23956989,55410753,106169956,93963675,47793464,100778354
1988,183232441,209894838,275121076,5434867,23089202,8210275,14404448,15589603,11640586,11342799,20506939,12346640,34209429,12862589,15862003,9660136,29036437,17891635,21808613,27309085,23358966,29585374,37809110,26565345,42572024,44540723,18516964,29328931,29455664,57044790,30497993,25327851,56925349,107471421,94519909,50281090,103903008
1989,185333919,216442440,287504237,5728511,23241946,8591294,14805506,16067828,11877599,11895419,20883649,12878973,34541910,13237507,16267069,10107028,29169746,18505772,22398271,27844463,23893893,30626132,37826147,27860539,42846787,44628280,19464440,30587388,30682120,57469536,31695621,26761923,58445699,108424260,95061557,52840795,107023058
1990,187966119,223096279,300165618,6043927,23350476,8977771,15207438,16547195,12142167,12432320,21206427,13416507,34641542,13772504,16641681,10602701,29286916,19149881,23000000,28373007,24406147,31656393,37846480,29082060,43127028,44733264,20439396,31751090,31923300,58079842,32914428,28259055,59951345,108837430,95542280,55483475,110146163
1991,191509147,229752408,314301034,6383315,23450653,9366643,15608008,17014346,12437110,12952377,21482271,13958197,34734234,14324149,16936803,11155614,29430524,19822817,23613619,28887229,24890671,32459655,37861339,29760929,43467991,44855736,21391215,32799183,32891192,58625381,34148626,29472185,61411745,109073667,96005316,58355038,113265899
1992,195199459,236274335,328521540,6744717,23539562,9846036,16007183,17476497,12759904,13454748,21813814,14502589,34854630,14889784,17206294,11762423,29607126,20511177,24238652,29352243,25351104,33169705,37907512,30439167,43785894,45011752,22255441,33888647,33738148,59146205,35400161,30726608,62876086,109115423,96414127,61396542,116379447
1993,198806845,242896345,342961393,7125617,23616843,10348234,16406525,17938345,13107820,13888412,22130869,15044498,34894325,15462867,17462829,12400106,29793326,21212379,24870590,29809452,25795989,33872973,37964793,31119331,44077099,45154426,23136115,34949749,34589980,59647492,36682088,32024149,64340297,108985461,96821759,64522965,119489196
1994,202278113,249629427,357836540,7519515,23684074,10868160,16820251,18400682,13475501,14285427,22450573,15579364,34741407,16036816,17724400,13037939,29967941,21906214,25506165,30260590,26238204,34575766,38006634,31802962,44342923,45304828,24044693,35978707,35453793,60028206,38012967,33368626,65800649,108843023,97320876,67741187,122608542
1995,205718394,256470883,373035157,7888014,23733222,11409139,17240497,18863803,13858715,14663134,22760570,16103497,34487999,16604653,18004312,13652804,30133075,22576687,26142216,30705908,26687693,35280305,38041344,32491471,44604027,45459651,24983813,37010603,36333541,60378049,39405594,34764761,67251519,108866238,97862490,71046004,125743641
1996,209146726,263440887,388593258,8247227,23768665,11973524,17668537,19326875,14255733,15019607,23081461,16615709,34203211,17163390,18309040,14233459,30291556,23218617,26777679,31145811,27145811,35810625,38086153,33183963,44863823,45610451,25958281,38054098,37230469,60725922,40868934,36216363,68621427,108700578,98272808,74433222,128896593
1997,212694273,270523260,404485562,8610539,23801620,12557524,18100961,19788904,14665741,15360171,23428320,17115300,33912723,17713643,18632440,14788167,30451227,23836117,27410868,31579645,27681745,36259672,38140443,33879027,45123550,45762480,26961627,39051703,38142785,60986289,42394458,37721269,69944968,108515107,98667335,77897525,132175402
1998,216206090,277707327,420606126,8981735,23827143,13157335,18529226,20251502,15123544,15704580,23739358,17606301,33630036,18256423,18965132,15338240,30610090,24431488,28043543,32009566,28270163,36633453,38185563,34576876,45390693,45931122,27993765,40038693,39068844,61167004,43938667,39284672,71260342,108329743,99095072,81452538,135456370
1999,219721677,284978106,436766058,9363839,23842562,13764926,18940925,20715860,15606189,16078075,24048329,18103945,33333798,18823973,19293244,15915279,30767125,25011815,28676311,32438320,28862915,37006183,38226137,35277843,45743988,46119137,29046503,41030621,40002835,61376500,45445310,40910972,72568684,107987645,99434460,85098300,138709641
2000,223069137,292322757,452999147,9761536,23611695,14375105,19326872,21182664,16094906,16499665,24388404,18600197,33019123,19477364,19758316,16544508,30937864,25582579,29309135,32867357,29457843,37428328,38277624,35981496,46214923,46319551,30114488,42026541,40942334,61629857,46947759,42603693,73899942,107528575,99760751,88840036,141915773
2001,225792302,300118524,471767321,10174157,23622394,14918252,19683539,21651926,16594707,16978440,24757782,19094843,32707281,20134757,20680738,17234266,31186430,26143328,29941986,33297967,30055505,37867709,38333314,36689358,46709571,46557334,31346174,43031116,41943212,61902439,48435634,44726802,75268749,107067618,101706485,91738482,144508698
2002,228400290,308796507,491993700,10604081,23621395,15458510,20014065,22057235,17104294,17510539,25052940,19591211,32432088,20800243,21613066,17981229,31708814,26720456,30573543,33728934,30659220,38258247,38447500,37399541,47206863,46930583,32796275,44050010,42979004,62174878,49919690,46947855,76634718,106568907,104055019,94698443,147005321
2003,230876596,317584393,512473984,11049316,23563051,15999895,20323604,22460389,17607000,18084339,25304780,20095408,32238838,21474148,22554369,18783707,32390830,27305501,31200433,34159485,31267885,38626122,38686985,38104244,47699501,47323791,34276157,45078311,44016864,62376854,51408755,49272800,78013778,106132766,106256267,97720468,149452654
2004,233532722,326495067,533257098,11510093,23506503,16548747,20619818,22858285,18080764,18680566,25566897,20616005,32075858,22163653,23508150,19635486,33059302,27898739,31817837,34588886,31879899,38947802,39006818,38793193,48209404,47767769,35772637,46107032,45048098,62529997,52922901,51708640,79428213,105771150,108136910,100811250,151843988
2005,236200507,335503762,554367818,11986371,23453429,17108405,20909083,23248394,18513378,19286107,25834340,21159469,31932581,22870388,24472850,20534304,33727737,28506195,32422505,35015695,32495526,39195731,39267369,39459729,48730233,48269624,37274029,47130046,46065713,62660267,54474508,54260116,80890450,105433013,109856670,103961908,154176524
2006,238999326,344622641,575118254,12478999,23396235,17681869,21194852,23622801,18889524,19893608,26126316,21729732,31801166,23598140,25451682,21483246,34408810,29131012,33015422,35439470,33111793,39490771,39454178,40099208,49228316,48798541,38777278,48145863,47064036,62753557,56069495,56934076,82408287,105152847,111383848,107178769,156447985
2007,241795278,353850625,595670841,13001478,23340838,18266045,21477303,23979532,19223314,20507062,26441461,22327433,31694727,24348000,26439736,22487132,35159317,29774433,33597410,35861110,33700835,39740941,39722857,40715257,49690034,49351705,40283010,49125150,48048353,62833410,57704173,59734515,83973950,105037426,112827761,110459097,158660052
2008,244607104,363154577,616481190,13689470,23300939,18854125,21722213,24328099,19565443,21137735,26789863,22955890,31605915,25120981,27438708,23545867,35833174,30442138,34166611,36284569,34289703,40093884,40056298,41319488,50127513,49913475,41802031,50093529,49047262,62875807,59373384,62665438,85573769,105055515,114107975,113814309,160823508
2009,247276259,372465917,637407288,14413055,23274004,19435329,21951808,24683266,19985687,21801430,27158023,23612219,31547432,25920396,28447159,24661566,36260460,31137754,34720224,36714245,34919606,40351067,40308358,41929228,50547198,50463084,43352798,51080202,50096864,62877220,61070625,65723902,87182005,105149982,115228215,117243827,162949977
2010,249845296,381763166,658498663,15178365,23165018,20002882,22185851,25050752,20552598,22508632,27522537,24299173,31465465,26757120,29469051,25833935,36535850,31866171,35257426,37055902,35603056,40602657,40502481,42597021,50961407,51030310,44954427,52095517,51225589,62940432,62793242,68917190,88781439,105261473,116302928,120709130,165055094
2011,252186596,391040303,678933504,15986316,23134846,20516662,22421443,25452964,21282801,23264870,27847821,25016303,31395063,27621281,30179366,27065209,36773882,32630538,35770189,37543830,36347578,40909592,40641670,43505397,51373800,51600211,46610737,53140089,52440387,61940177,64539555,72230098,90370891,105407907,116416235,124016544,167158525
2012,254576561,400417728,699187267,16839218,23086831,21009012,22662393,25875071,22141872,24058860,28166078,25763106,31359984,28514086,30828051,28355103,36904876,33429132,36262940,38027774,37114895,41089082,40894259,44437841,51791144,52130345,48311928,54203628,53730762,62064608,66312625,75664328,91948801,105669949,116331281,127396459,169240750
2013,256899730,409909187,719587859,17717910,23025350,21491648,22922695,26249155,23075649,24865453,28479640,26536378,31330962,29430443,31477989,29704531,36891840,34249974,36761204,38509756,37919402,41240244,41548775,45385009,52228061,52650595,50048763,55292621,55071747,62242278,68114463,79214937,93515787,105998510,116262976,130826016,171300683
2014,259361210,419568459,740239259,18635946,22960228,21974918,23221633,26482983,24005089,25648738,28781576,27332602,31223156,30371288,32127193,31115109,36890017,35079618,37300597,38990109,38737018,41463573,42109853,46329521,52647802,53209683,51818338,56418308,56438761,62510392,69956952,82878551,95072126,106354643,116208079,134287151,173346772
2015,261865944,429428653,761027100,19590313,22897449,22464989,23571246,26518336,24872481,26382755,29011826,28146511,31183824,31333193,32772699,32586918,36971015,35905874,37904385,39467043,39551544,41645542,42247229,47262079,53009026,53802927,53608403,57580319,57806283,63062064,71845558,86652532,96615320,106703732,116182717,137751865,175375436
2016,264366216,439498772,782199374,20581872,22849639,22964507,23979736,26313884,25661856,27057429,29357013,28977628,31122512,32314724,33415230,34121207,37112875,36724030,38589139,39940546,40359123,41774264,42351339,48177810,53321626,54382825,55420910,58781895,59175037,63592936,73782310,90531047,98145001,107050095,116145370,141210511,177386818
2017,266676634,449789369,803554542,21609845,22824769,23467810,24438508,25917344,26391343,27678169,29727992,29821745,31043768,33310316,34051250,35717778,37311863,37534797,39338952,40410674,41185809,41861498,42462869,49096983,53612472,54923317,57254681,60016579,60537696,63861626,75761710,94518555,99655905,107349518,116053379,144652795,179379301
2018,268720071,460295677,823827650,22678295,22806875,23973075,24921870,25465822,27085311,28255384,30169097,30670086,30946607,34317154,34678853,37376673,37587449,38339668,40105215,40877099,42030812,42038247,42559879,50027217,53879064,55426598,59107944,61266765,61857510,64096118,77810763,98611179,101149488,107539347,115920900,148084795,181335507

城市人口图例程

# @Time    : 2022/1/12 22:14
# @Author  : 南黎
# @FileName: 8.城市人口.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas_alive

urban_df = pd.read_csv("数据源data/urban_pop.csv", index_col=0, parse_dates=[0], thousands=',')

animated_line_chart = (
    urban_df
        .sum(axis=1)  # 按行计算的和
        .pct_change()  # 计算与前一个元素的百分比 比如说[1,2,3].pct_change()=[NaN,(2-1)/1,(3-2)/2] 为什么第一个是NaN空值呢,因为第一数前面没有数给他减,给他除了···
        .fillna(method='bfill')  # backfill/bfill用下一个非缺失值填充该缺失值,因为前一步计算会有缺失值
        .mul(100)  # 缺失值单元格在相乘之前已填充100 函数返回数据帧和其他元素的乘法。此功能本质上与dataframe * other,但它提供了额外的支持来处理其中一个输入中的缺失值。
        .plot_animated(
        kind="line",  # 图表类型为折线图
        title="子图1",  # 子图名称
        period_label=False,  # 不显示时间
        add_legend=False  # 不显示渲染图进度条
    )

)

animated_bar_chart = urban_df.plot_animated(
    n_visible=10,  # 属性参数取10个
    title="子图2",  # 子图名称
    period_fmt="%Y"  # 时间格式,XXXX年
)

pandas_alive.animate_multiple_plots(
    filename='8.城市人口.gif',
    plots=[animated_bar_chart, animated_line_chart],  # 两个表放进一个列表中
    title='发现你走远了——8.城市人口',
    enable_progress_bar=True,
    adjust_subplot_top=0.85,

    # 设置子图的位置
    # adjust_subplot_left=0.5,
    # adjust_subplot_right=0.9,
    # adjust_subplot_bottom=0.1,
    # adjust_subplot_top=0.9,
    # adjust_subplot_wspace=0.2,
    # adjust_subplot_hspace=0.25
)


9.G7国家的预期寿命

内容和前面的图8.城市人口 类似,主要是更多的综合应用,结合了数据处理(从网络上获取数据并转化成我们需要的数据)

G7国家的预期寿命图API说明:

data_raw = data_raw.pivot(
index=“Year”, columns=“Entity”, values=“Life expectancy (Gapminder, UN)”
)

  • 重塑数据(产生一个“pivot”表格)以列值为标准。使用来自索引/列的唯一的值(去除重复值)为轴形成dataframe结果。

G7国家的预期寿命图数据集

数据集文件名为 Life Expectancy in G7 Countries,码云下载

data_raw = pd.read_csv("数据源data/Life Expectancy in G7 Countries.csv")
# 原作者是在线获取,这里直接用已经下载好的csv
# data_raw = pd.read_csv(
#     "https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN.csv"
# )

G7国家的预期寿命图例程

# @Time    : 2022/1/12 22:15
# @Author  : 南黎
# @FileName: 9.G7国家的预期寿命.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas_alive
import pandas as pd

data_raw = pd.read_csv("数据源data/Life Expectancy in G7 Countries.csv")
# 原作者是在线获取,这里直接用已经下载好的csv
# data_raw = pd.read_csv(
#     "https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN/Long%20run%20life%20expectancy%20-%20Gapminder%2C%20UN.csv"
# )


list_G7 = [
    "Canada",
    "France",
    "Germany",
    "Italy",
    "Japan",
    "United Kingdom",
    "United States",
]
#重塑数据(产生一个“pivot”表格)以列值为标准。使用来自索引/列的唯一的值(去除重复值)为轴形成dataframe结果。
data_raw = data_raw.pivot(
    index="Year", columns="Entity", values="Life expectancy (Gapminder, UN)"
)

data = pd.DataFrame()
data["Year"] = data_raw.reset_index()["Year"]
#因为原始网页数据集有很多国家,这里选择我们需要的7个国家
for country in list_G7:
    data[country] = data_raw[country].values

data = data.fillna(method="pad")
data = data.fillna(0)
data = data.set_index("Year").loc[1900:].reset_index()

data["Year"] = pd.to_datetime(data.reset_index()["Year"].astype(str))

data = data.set_index("Year")

animated_bar_chart = data.plot_animated(
    period_fmt="%Y",#动态更新图中时间戳
    perpendicular_bar_func="mean", #设置平均值辅助线
    period_length=200,#周期长度 200ms
    fixed_max=True,

)

animated_line_chart = data.plot_animated(
    kind="line",
    period_fmt="%Y",
    period_length=200,
    fixed_max=True
)

pandas_alive.animate_multiple_plots(
    filename="9.G7国家的预期寿命.gif",
    plots=[animated_bar_chart, animated_line_chart],
    title="发现你走远了——9.G7国家的预期寿命",
    adjust_subplot_left=0.2,
    adjust_subplot_top=0.9,
    enable_progress_bar=True
)


10.新南威尔士州 COVID 可视化

数据集接口异常,我没有绘制成功。····

新南威尔士州 COVID 可视化API说明:

新南威尔士州 COVID 可视化数据集

新南威尔士州 COVID 可视化例程

# @Time    : 2022/1/12 22:16
# @Author  : 南黎
# @FileName: 10.新南威尔士州 COVID 可视化.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################


import geopandas
import pandas as pd
import pandas_alive
import contextily
import matplotlib.pyplot as plt

import urllib.request, json

with urllib.request.urlopen(
    "https://data.nsw.gov.au/data/api/3/action/package_show?id=aefcde60-3b0c-4bc0-9af1-6fe652944ec2"
) as url:
    data = json.loads(url.read().decode())
print(data)
# Extract url to csv component
covid_nsw_data_url = data["result"]["resources"][0]["url"]

# Read csv from data API url
nsw_covid = pd.read_csv(covid_nsw_data_url)
postcode_dataset = pd.read_csv("数据源data/postcode-data.csv")

# Prepare data from NSW health dataset


nsw_covid = nsw_covid.fillna(9999)
nsw_covid["postcode"] = nsw_covid["postcode"].astype(int)

grouped_df = nsw_covid.groupby(["notification_date", "postcode"]).size()
grouped_df = pd.DataFrame(grouped_df).unstack()
grouped_df.columns = grouped_df.columns.droplevel().astype(str)

grouped_df = grouped_df.fillna(0)
grouped_df.index = pd.to_datetime(grouped_df.index)

cases_df = grouped_df

# Clean data in postcode dataset prior to matching

grouped_df = grouped_df.T
postcode_dataset = postcode_dataset[postcode_dataset['Longitude'].notna()]
postcode_dataset = postcode_dataset[postcode_dataset['Longitude'] != 0]
postcode_dataset = postcode_dataset[postcode_dataset['Latitude'].notna()]
postcode_dataset = postcode_dataset[postcode_dataset['Latitude'] != 0]
postcode_dataset['Postcode'] = postcode_dataset['Postcode'].astype(str)

# Build GeoDataFrame from Lat Long dataset and make map chart
grouped_df['Longitude'] = grouped_df.index.map(postcode_dataset.set_index('Postcode')['Longitude'].to_dict())
grouped_df['Latitude'] = grouped_df.index.map(postcode_dataset.set_index('Postcode')['Latitude'].to_dict())
gdf = geopandas.GeoDataFrame(
    grouped_df, geometry=geopandas.points_from_xy(grouped_df.Longitude, grouped_df.Latitude),crs="EPSG:4326")
gdf = gdf.dropna()

# Prepare GeoDataFrame for writing to geopackage
gdf = gdf.drop(['Longitude','Latitude'],axis=1)
gdf.columns = gdf.columns.astype(str)
gdf['postcode'] = gdf.index
gdf.to_file("数据源data/nsw-covid19-cases-by-postcode.gpkg", layer='nsw-postcode-covid', driver="GPKG")

# Prepare GeoDataFrame for plotting
gdf.index = gdf.postcode
gdf = gdf.drop('postcode',axis=1)
gdf = gdf.to_crs("EPSG:3857") #Web Mercator

map_chart = gdf.plot_animated(basemap_format={'source':contextily.providers.Stamen.Terrain},cmap='cool')

cases_df.to_csv('数据源data/nsw-covid-cases-by-postcode.csv')

from datetime import datetime

bar_chart = cases_df.sum(axis=1).plot_animated(
    kind='line',
    label_events={
        'Ruby Princess Disembark':datetime.strptime("19/03/2020", "%d/%m/%Y"),
        'Lockdown':datetime.strptime("31/03/2020", "%d/%m/%Y")
    },
    fill_under_line_color="blue",
    add_legend=False
)

map_chart.ax.set_title('Cases by Location')

grouped_df = pd.read_csv('数据源data/nsw-covid-cases-by-postcode.csv', index_col=0, parse_dates=[0])

line_chart = (
    grouped_df.sum(axis=1)
    .cumsum()
    .fillna(0)
    .plot_animated(kind="line", period_label=False, title="Cumulative Total Cases", add_legend=False)
)


def current_total(values):
    total = values.sum()
    s = f'Total : {int(total)}'
    return {'x': .85, 'y': .2, 's': s, 'ha': 'right', 'size': 11}

race_chart = grouped_df.cumsum().plot_animated(
    n_visible=5, title="Cases by Postcode", period_label=False,period_summary_func=current_total
)

import time

timestr = time.strftime("%d/%m/%Y")

plots = [bar_chart, line_chart, map_chart, race_chart]

from matplotlib import rcParams

rcParams.update({"figure.autolayout": False})
# make sure figures are `Figure()` instances
figs = plt.Figure()
gs = figs.add_gridspec(2, 3, hspace=0.5)
f3_ax1 = figs.add_subplot(gs[0, :])
f3_ax1.set_title(bar_chart.title)
bar_chart.ax = f3_ax1

f3_ax2 = figs.add_subplot(gs[1, 0])
f3_ax2.set_title(line_chart.title)
line_chart.ax = f3_ax2

f3_ax3 = figs.add_subplot(gs[1, 1])
f3_ax3.set_title(map_chart.title)
map_chart.ax = f3_ax3

f3_ax4 = figs.add_subplot(gs[1, 2])
f3_ax4.set_title(race_chart.title)
race_chart.ax = f3_ax4

timestr = cases_df.index.max().strftime("%d/%m/%Y")
figs.suptitle(f"NSW COVID-19 Confirmed Cases up to {timestr}")

pandas_alive.animate_multiple_plots(
    '10.新南威尔士州 COVID 可视化.gif',
    plots,
    figs,
    enable_progress_bar=True
)


总结

欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
【Python安装第三方库一行命令永久提高速度】
【使用PyInstaller打包Python文件】
【更多内容敬请期待】


你可能感兴趣的:(#,python,pandas_alive,数据可视化,动态绘图)