Python可视化库Pandas_Alive,动态图表随意做

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

以下文章一级法纳斯特 ,作者小F

9e85bf59937f4b2aabf5b4a271df5c12~tplv-tt-shrink:640:0.image

 

前言

最近发现汉语中类似的一个可视化图库「Pandas_Alive」,不仅包含动态条形图,还可以绘制动态曲线图产品,气泡图,饼状图,地图在等。

同样也是几行代码就能完成动态图表的替换。

GitHub地址:

https://github.com/JackMcKew/pandas_alive

使用文档:

https://jackmckew.github.io/pandas_alive/

安装版本建议是0.2.3, matplotlib版本是3.2.1。

同时需自行安装tqdm(显示进度条)和descartes(放置地图相关库)。

要不然会出现报错,估计是作者的requestment.txt没包含这两个库。

好了,成功安装后就可以约会这个第三方库,直接选择加载本地文件。

import  pandas_alive as  pd 
import  pandas  
covid_df = pd.read_csv('data / covid19.csv',index_col = 0,parse_dates = [ 0 ])
covid_df.plot_animated(filename = 'examples / example-barh-chart.gif',n_visible = 15)

 

生成了一个GIF图,具体如下。

80d26b03508e47bbb8e55c1ca2103c73~tplv-tt-shrink:640:0.image

 

刚开始学习这个库的时候,大家可以减少数据,这样生成GIF的时间就会快一些。

例如在接下来的实践中,基本都只挑选了20天左右的数据。

3d264e92457d433980829bcebc11e319~tplv-tt-shrink:640:0.image

 

对于其他图表,我们可以查看官方文档的API说明,得以了解。

cb8b495ea99647b2949d1e1b33550b81~tplv-tt-shrink:640:0.image

 

下面我们就来看看其他动态图表的替换方法吧!

动态条形图

elec_df = pd.read_csv(“ data / Aus_Elec_Gen_1980_2018.csv”,index_col = 0,parse_dates = [ 0 ],千元= '')
elec_df = elec_df.iloc [:20,:] elec_df.fillna(0).plot_animated('examples / example-electricity- generation -australia.gif',period_fmt = “%Y”,title = '1980-2018年澳大利亚发电来源'

 

4a8dad5691fa4cf69f5fd6d96708bda9~tplv-tt-shrink:640:0.image

 

02动态柱状图

covid_df = pd.read_csv('data / covid19.csv',index_col = 0,parse_dates = [ 0 ])
covid_df.plot_animated(filename = 'examples / example-barv-chart.gif',方向= 'v',n_visible = 15)

 

39268a864fbc4a22ba9e31a1713b0be3~tplv-tt-shrink:640:0.image

 

03动态曲线图

covid_df = pd.read_csv('data / covid19.csv',index_col = 0,parse_dates = [ 0 ])
covid_df.diff()
fillna(0).plot_animated(filename = 'examples / example-line-chart.gif',kind = 'line',period_label = { 'x':  0.25,  'y':  0.9 })

 

6bd36e5db8d34f73905d67852dbaf213~tplv-tt-shrink:640:0.image

 

04动态面积图

covid_df = pd.read_csv('data / covid19.csv',index_col = 0,parse_dates = [ 0 ])
covid_df.sum(axis = 1).fillna(0).plot_animated(filename = 'examples / example-bar-chart .gif',kind = 'bar',
        period_label = { 'x':  0.1,  'y':  0.9 },
        enable_progress_bar = True,steps_per_period = 2,interpolate_period = True,period_length = 200

 

90541881bdf74e3cb473035df7d133d9~tplv-tt-shrink:640:0.image

 

05动态散点图

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_Tustralia_T。,
    parse_dates = { “ Timestamp”:[ “ Year”,  “ Month”,  “ Day” ]},
)

max_temp_df = max_temp_df.iloc [:5000
,:] min_temp_df = min_temp_df.iloc [:5000

,:] 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 = [ “最低温度(摄氏度)”,  “最高温度(摄氏度)) “ ” 
merged_temp_df [keep_columns] .resample(“ Y”).mean()。plot_animated(filename = 'examples / example-scatter-chart.gif',kind = “ scatter”,
                                                                title = “最高温度和最低温度澳大利亚纽卡斯尔'

 

5e83da1b8f594b48a169981f96552dcc~tplv-tt-shrink:640:0.image

 

06动态饼状图

covid_df = pd.read_csv('data / covid19.csv',index_col = 0,parse_dates = [ 0 ])
covid_df.plot_animated(filename = 'examples / example-pie-chart.gif',kind = “ pie”,
                       rotationlabels = True,period_label = { 'x':  0,  'y':  0 })

 

11383bf196b04d82bea5fe456a01cf9f~tplv-tt-shrink:640:0.image

 

07动态气泡图

multi_index_df = pd.read_csv( “数据/ multi.csv” ,标题= [ 0,  1 ],index_col = 0)
multi_index_df.index = pd.to_datetime(multi_index_df.index,dayfirst =真)

map_chart = multi_index_df.plot_animated(
    种类= “ bubble”,
    文件名= “ examples / example-bubble-chart.gif”,
    x_data_label = “经度”,
    y_data_label = “纬度”,
    size_data_label = “案例”,
    color_data_label = “案例”,
    vmax = 5,steps_per_period = 3,interpolate_period = True,period_length = 500,
    dpi = 100

 

83dda74eac374c90acf6877fdd40d99d~tplv-tt-shrink:640:0.image

 

08地理空间点图表

进口 geopandas
导入 pandas_alive
进口 contextily 

GDF = geopandas.read_file('数据/ NSW-covid19-例逐postcode.gpkg' )
gdf.index = gdf.postcode 
GDF = gdf.drop('邮编',轴= 1)

的结果= gdf.iloc [:,:20 ] 
result [ 'geometry' ] = gdf.iloc [:,  -1:] [ 'geometry' ] 

map_chart = result.plot_animated(filename = 'examples / example-geo-point-chart .gif”,
                                 basemap_format = { 'source':contextily.providers.Stamen.Terrain})

 

86cc5e2912224ed494e189aa5275e14d~tplv-tt-shrink:640:0.image

 

09总体地理图表

进口 geopandas
导入 pandas_alive
进口 contextily 

GDF = geopandas.read_file('数据/意大利-covid-region.gpkg' )
gdf.index = gdf.region 
GDF = gdf.drop('区域',轴= 1)

结果= gdf.iloc [:,:20 ] 
result [ 'geometry' ] = gdf.iloc [:,  -1:] [ 'geometry' ] 

map_chart = result.plot_animated(filename = 'examples / example-example-example-geo-polygon-chart.gif',
                                 basemap_format = { 'source':contextily.providers.Stamen.Terrain})

 

c9d6f86730ba45b1aad2b1a66707e769~tplv-tt-shrink:640:0.image
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值