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

小鱼儿 2022-12-28 06:12 367阅读 0赞

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

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

bb3a14fcea2cbce0e66349aa690033c6.png

前言

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

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

GitHub地址:

  1. https://github.com/JackMcKew/pandas_alive

使用文档:

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

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

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

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

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

  1. import pandas_alive as pd
  2. import pandas
  3. covid_df = pd.read_csv'data / covid19.csv'index_col = 0parse_dates = [ 0 ])
  4. covid_df.plot_animatedfilename = 'examples / example-barh-chart.gif'n_visible = 15

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

Python可视化库Pandas\_Alive,动态图表随意做

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

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

e2945be2ea7282875edef4cc23569b44.png

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

d8e946b7c6001b9d66cdb17364badf7c.png

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

动态条形图

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

e919ae94b5959622c318ff93324943c7.gif

02动态柱状图

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

d60e8cb98b06454e1b12adda7f33196a.gif

03动态曲线图

  1. covid_df = pd.read_csv'data / covid19.csv'index_col = 0parse_dates = [ 0 ])
  2. covid_df.diff()
  3. fillna0).plot_animatedfilename = 'examples / example-line-chart.gif'kind = 'line'period_label = { 'x' 0.25 'y' 0.9 })

4f70dc2a4c23fe18ba95fea9aa5f6c95.gif

04动态面积图

  1. covid_df = pd.read_csv'data / covid19.csv'index_col = 0parse_dates = [ 0 ])
  2. covid_df.sumaxis = 1).fillna0).plot_animatedfilename = 'examples / example-bar-chart .gif'kind = 'bar'
  3. period_label = { 'x' 0.1 'y' 0.9 },
  4. enable_progress_bar = Truesteps_per_period = 2interpolate_period = Trueperiod_length = 200

3a86017389b5b147209ecf6a0b131699.gif

05动态散点图

  1. max_temp_df = pd.read_csv
  2. data / Newcastle_Australia_Max_Temps.csv”,
  3. parse_dates = { Timestamp”:[ Year”, Month”, Day ]},
  4. min_temp_df = pd.read_csv
  5. data / Newcastle_Tustralia_T。,
  6. parse_dates = { Timestamp”:[ Year”, Month”, Day ]},
  7. max_temp_df = max_temp_df.iloc [:5000
  8. ,:] min_temp_df = min_temp_df.iloc [:5000
  9. ,:] merged_temp_df = pd merge_asofmax_temp_dfmin_temp_dfon = Timestamp”)
  10. merged_temp_df.index = pd.to_datetimemerged_temp_df [ Timestamp ] .dt.strftime'%Y /%m /%d'))
  11. keep_columns = [ “最低温度(摄氏度)”, “最高温度(摄氏度))
  12. merged_temp_df [keep_columns] .resample(“ Y”).mean()。plot_animatedfilename = 'examples / example-scatter-chart.gif'kind = scatter”,
  13. title = “最高温度和最低温度澳大利亚纽卡斯尔')

23353b25b2f012dabb5ccdf59f511820.gif

06动态饼状图

  1. covid_df = pd.read_csv'data / covid19.csv'index_col = 0parse_dates = [ 0 ])
  2. covid_df.plot_animatedfilename = 'examples / example-pie-chart.gif'kind = pie”,
  3. rotationlabels = Trueperiod_label = { 'x' 0 'y' 0 })

5b96f913d58f95f2434c16e244487de0.gif

07动态气泡图

  1. multi_index_df = pd.read_csv “数据/ multi.csv ,标题= [ 0 1 ],index_col = 0
  2. multi_index_df.index = pd.to_datetimemulti_index_df.indexdayfirst =真)
  3. map_chart = multi_index_df.plot_animated
  4. 种类= bubble”,
  5. 文件名= examples / example-bubble-chart.gif”,
  6. x_data_label = “经度”,
  7. y_data_label = “纬度”,
  8. size_data_label = “案例”,
  9. color_data_label = “案例”,
  10. vmax = 5steps_per_period = 3interpolate_period = Trueperiod_length = 500
  11. dpi = 100

a34fd9c75f7c3e6907eec4b49b6daf19.gif

08地理空间点图表

  1. 进口 geopandas
  2. 导入 pandas_alive
  3. 进口 contextily
  4. GDF = geopandas.read_file'数据/ NSW-covid19-例逐postcode.gpkg'
  5. gdf.index = gdf.postcode
  6. GDF = gdf.drop'邮编',轴= 1
  7. 的结果= gdf.iloc [:,:20 ]
  8. result [ 'geometry' ] = gdf.iloc [:, -1:] [ 'geometry' ]
  9. map_chart = result.plot_animatedfilename = 'examples / example-geo-point-chart .gif”,
  10. basemap_format = { 'source':contextily.providers.Stamen.Terrain})

c22878e4bc5d490c47dda84dd755a3d5.gif

09总体地理图表

  1. 进口 geopandas
  2. 导入 pandas_alive
  3. 进口 contextily
  4. GDF = geopandas.read_file'数据/意大利-covid-region.gpkg'
  5. gdf.index = gdf.region
  6. GDF = gdf.drop'区域',轴= 1
  7. 结果= gdf.iloc [:,:20 ]
  8. result [ 'geometry' ] = gdf.iloc [:, -1:] [ 'geometry' ]
  9. map_chart = result.plot_animatedfilename = 'examples / example-example-example-geo-polygon-chart.gif'
  10. basemap_format = { 'source'contextily.providers.Stamen.Terrain})

5a1fccefe990728cb22a0e57285294fa.gif

发表评论

表情:
评论列表 (有 0 条评论,367人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Pythonmatplotlib

    绘制基本曲线: 使用plot函数绘制函数曲线,可以调整plot函数参数,配置曲线样式、粗细、颜色、标记等。 设置坐标轴: (1)spines方法移动坐标轴 (2)