Python可视化库Pandas_Alive,动态图表随意做[Python基础]
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
以下文章一级法纳斯特 ,作者小F
前言
最近发现汉语中类似的一个可视化图库「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 pandascovid_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图,具体如下。
刚开始学习这个库的时候,大家可以减少数据,这样生成GIF的时间就会快一些。
例如在接下来的实践中,基本都只挑选了20天左右的数据。
对于其他图表,我们可以查看官方文档的API说明,得以了解。
下面我们就来看看其他动态图表的替换方法吧!
动态条形图
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年澳大利亚发电来源")
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)
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 })
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)
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
= “最高温度和最低温度澳大利亚纽卡斯尔")
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 })
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)
08地理空间点图表
进口 geopandas导入 pandas_alive
进口 contextily
GDF
= geopandas.read_file("数据/ NSW-covid19-例逐postcode.gpkg" )gdf.index
= gdf.postcodeGDF
= 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})
09总体地理图表
进口 geopandas导入 pandas_alive
进口 contextily
GDF
= geopandas.read_file("数据/意大利-covid-region.gpkg" )gdf.index
= gdf.regionGDF
= 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})
以上是 Python可视化库Pandas_Alive,动态图表随意做[Python基础] 的全部内容, 来源链接: utcz.com/z/530758.html