Python繪制數(shù)據(jù)動態(tài)圖的方法詳解
數(shù)據(jù)動態(tài)圖怎么做,效果圖,

多子圖聯(lián)動競賽圖
安裝
pip?install?pandas_alive #或者 conda?install?pandas_alive?-c?conda-forge
玩起來
支持?jǐn)?shù)據(jù)
數(shù)據(jù)格式如下,

使用方法類似pandas??這些,pandas僅需一行代碼解決支持圖形類別

動態(tài)地圖
結(jié)合geopandas,

動態(tài)水平bar
import?pandas?as?pd
import?pandas_alive
import?matplotlib.pyplot?as?plt
plt.style.use('ggplot')
#讀入數(shù)據(jù)
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",??#動態(tài)更新圖中時間戳
????title='Australian?Electricity?Sources?1980-2018',??#標(biāo)題
????perpendicular_bar_func='mean',??#添加均值輔助線
????period_summary_func=current_total,??#匯總
????cmap='Set1',??#定義調(diào)色盤
????n_visible=5,??#柱子顯示數(shù)
????orientation='h',#柱子方向
)

動態(tài)垂直bar

動態(tài)折線
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')

動態(tài)累積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',??#定義調(diào)色盤
????period_label={
????????'x':?0.1,
????????'y':?0.9
????},
????orientation='h',
????enable_progress_bar=True,
????steps_per_period=2,
????interpolate_period=True,
????period_length=200)

動態(tài)散點圖
import?pandas?as?pd
import?pandas_alive
#max散點數(shù)據(jù)
max_temp_df?=?pd.read_csv(
????"Newcastle_Australia_Max_Temps.csv",
????parse_dates={"Timestamp":?["Year",?"Month",?"Day"]},
)
#min散點數(shù)據(jù)
min_temp_df?=?pd.read_csv(
????"Newcastle_Australia_Min_Temps.csv",
????parse_dates={"Timestamp":?["Year",?"Month",?"Day"]},
)
#按時間戳merge?max/min數(shù)據(jù)
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')

動態(tài)氣泡圖
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)

多子圖一起動
這部分可以結(jié)合matplotlib的多子圖繪制,實現(xiàn)各種個性化動圖,可參考matplotlib-多子圖繪制(為所欲為版),核心代碼如下,


到此這篇關(guān)于Python繪制數(shù)據(jù)動態(tài)圖的方法詳解的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)動態(tài)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python list和str互轉(zhuǎn)的實現(xiàn)示例
這篇文章主要介紹了Python list和str互轉(zhuǎn)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python+樹莓派+YOLO打造一款人工智能照相機(jī)
今天,我們將自己動手打造出一款基于深度學(xué)習(xí)的照相機(jī),當(dāng)小鳥出現(xiàn)在攝像頭畫面中時,它將能檢測到小鳥并自動進(jìn)行拍照2018-01-01
Python-copy()與deepcopy()區(qū)別詳解
這篇文章主要介紹了Python-copy()與deepcopy()區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
使用pytorch搭建AlexNet操作(微調(diào)預(yù)訓(xùn)練模型及手動搭建)
今天小編就為大家分享一篇使用pytorch搭建AlexNet操作(微調(diào)預(yù)訓(xùn)練模型及手動搭建),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
使用Python對微信好友進(jìn)行數(shù)據(jù)分析
這篇文章主要介紹了使用Python對微信好友進(jìn)行數(shù)據(jù)分析的實現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06

