可能是Python最好用的动态图工具了

大邓和他的Python

共 8386字,需浏览 17分钟

 · 2021-09-07

开学特惠| Python网络爬虫与文本分析


前面介绍过火遍各网站的动态柱状图做法👉来呀!把玩一下条形竞赛图,但是,之前的方法仅限于条形图,有小伙伴说,有没有能让地图、散点图、饼图、散点图、气泡图等也动起来的法子,且看本文,效果图,

多子图联动竞赛图

安装

pip install pandas_alive

#或者
conda install pandas_alive -c conda-forge

玩起来

支持数据数据格式如下,使用方法类似pandas👉这些,pandas仅需一行代码解决
支持图形类别动态地图
结合geopandas,动态水平bar

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

plt.style.use('ggplot')

#读入数据
elec_df = pd.read_csv("Aus_Elec_Gen_1980_2018.csv",
                      index_col=0,
                      parse_dates=[0],
                      thousands=',')

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


#缺省值0填充、绘图
elec_df.fillna(0).tail(n=10).plot_animated(
    'electricity-generated-australia.gif',  #保存gif名称
    period_fmt="%d/%m/%Y",  #动态更新图中时间戳
    title='Australian Electricity Sources 1980-2018',  #标题
    perpendicular_bar_func='mean',  #添加均值辅助线
    period_summary_func=current_total,  #汇总
    cmap='Set1',  #定义调色盘
    n_visible=5,  #柱子显示数
    orientation='h',#柱子方向
)

动态垂直bar动态折线

elec_df.diff().fillna(0).tail(n=10).plot_animated(filename='line-chart.gif',
                                                 kind='line',#指定折线模式
                                                 cmap='Set1',
                                                 period_label={
                                                     'x'0.25,
                                                     'y'0.9
                                                 },
                                                 line_width=1,
                                                 add_legend=True,
                                                 fill_under_line_color='#01a2d9')

动态累积bar

import pandas_alive
covid_df.sum(axis=1).fillna(0).tail(n=10).plot_animated(
    filename='sumbar-chart.gif',
    kind='bar',   #指定bar模式
    cmap='Set1',  #定义调色盘
    period_label={
        'x'0.1,
        'y'0.9
    },
    orientation='h',
    enable_progress_bar=True,
    steps_per_period=2,
    interpolate_period=True,
    period_length=200)

动态散点图

import pandas as pd
import pandas_alive

#max散点数据
max_temp_df = pd.read_csv(
    "Newcastle_Australia_Max_Temps.csv",
    parse_dates={"Timestamp": ["Year""Month""Day"]},
)

#min散点数据
min_temp_df = pd.read_csv(
    "Newcastle_Australia_Min_Temps.csv",
    parse_dates={"Timestamp": ["Year""Month""Day"]},
)

#按时间戳merge max/min数据
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.head(n=5000)[keep_columns].resample("Y").mean().plot_animated(
    filename='scatter-chart.gif',
    cmap='Set1'
    kind="scatter",#指定散点模式
    size=10,
    title='Max & Min Temperature Newcastle, Australia')

动态气泡图

import pandas_alive

multi_index_df = pd.read_csv("multi.csv", header=[01], index_col=0)

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

map_chart = multi_index_df.tail(n=40).plot_animated(
    kind="bubble",  #指定气泡模式
    filename="bubble-chart.gif",
    x_data_label="Longitude",
    y_data_label="Latitude",
    size_data_label="Cases",
    color_data_label="Cases",
    vmax=5,
    steps_per_period=1,
    interpolate_period=True,
    period_length=500,
    dpi=150)

多子图一起动
这部分可以结合matplotlib的多子图绘制,实现各种个性化动图,可参考👉matplotlib-多子图绘制(为所欲为版),核心代码如下,上图完整代码获取关注公众号:pythonic生物人,留言获取!

更多个性化设置

方法一:参考之前的这个文章👉来呀!把玩一下条形竞赛图,参数大同小异。
方法二:https://github.com/JackMcKew/pandas_alive

文中数据获取

https://github.com/JackMcKew/pandas_alive/tree/main/data


开学特惠| Python网络爬虫与文本分析


近期文章

腾讯课堂 | Python网络爬虫与文本分析
中文金融情感词典发布啦 | 附代码
B站视频 | Python自动化办公
wordexpansion包 | 新增词向量法构建领域词典
语法最简单的微博通用爬虫weibo_crawler
hiResearch 定义自己的科研首页
大邓github汇总, 觉得有用记得star
whatlies包 | 简单玩转词向量可视化
multistop ~ 多语言停用词库
Jaal 库 轻松绘制动态社交网络关系图
SciencePlots | 科研样式绘图库
使用streamlit上线中文文本分析网站
爬虫实战 | 采集&可视化知乎问题的回答
Clumper | dplyr式的Python数据操作包
Clumper库 | 常用的数据操作函数
Clumper库 | Groupby具体案例用法
Clumper库 | 其他数据分析
plydata库 | 数据操作管道操作符>>
plotnine: Python版的ggplot2作图库
读完本文你就了解什么是文本分析
文本分析在经管领域中的应用概述  
综述:文本分析在市场营销研究中的应用
plotnine: Python版的ggplot2作图库
Wow~70G上市公司定期报告数据集
漂亮~pandas可以无缝衔接Bokeh  
YelpDaset: 酒店管理类数据集10+G

浏览 38
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报