前面介紹過火遍各網站的動態柱狀圖做法👉來呀!!!把玩一下條形競賽圖
但是,之前的方法僅限于條形圖,有小伙伴說,有沒有能讓地圖、散點圖、餅圖、散點圖、氣泡圖等也動起來的法子,且看本文,效果圖,

安裝
pip install pandas_alive
#或者
conda install pandas_alive -c conda-forge
玩起來
支持資料
資料格式如下,

使用方法類似pandas👉這20+中圖形,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=[0, 1], 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
文中資料獲取
關注公眾號:pythonic生物人,回復關鍵字pad獲取!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/294484.html
標籤:python

