本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
以下文章一級法納斯特 ,作者小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 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圖,具體如下,
剛開始學習這個庫的時候,大家可以減少資料,這樣生成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.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})
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})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/234653.html
標籤:其他
