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

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

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

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

動(dòng)態(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",??#動(dòng)態(tài)更新圖中時(shí)間戳
????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',#柱子方向
)

動(dòng)態(tài)垂直bar

動(dòng)態(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')

動(dòng)態(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)

動(dòng)態(tài)散點(diǎn)圖
import?pandas?as?pd
import?pandas_alive
#max散點(diǎn)數(shù)據(jù)
max_temp_df?=?pd.read_csv(
????"Newcastle_Australia_Max_Temps.csv",
????parse_dates={"Timestamp":?["Year",?"Month",?"Day"]},
)
#min散點(diǎn)數(shù)據(jù)
min_temp_df?=?pd.read_csv(
????"Newcastle_Australia_Min_Temps.csv",
????parse_dates={"Timestamp":?["Year",?"Month",?"Day"]},
)
#按時(shí)間戳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",#指定散點(diǎn)模式
????size=10,
????title='Max?&?Min?Temperature?Newcastle,?Australia')

動(dòng)態(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)

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


到此這篇關(guān)于Python繪制數(shù)據(jù)動(dòng)態(tài)圖的方法詳解的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)動(dòng)態(tài)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)ping指定IP的示例
今天小編就為大家分享一篇Python實(shí)現(xiàn)ping指定IP的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
pytest配置文件pytest.ini的配置、原理與實(shí)際應(yīng)用詳解
在Python的測試生態(tài)中,pytest無疑是最受歡迎的測試框架之一,在pytest的眾多配置文件中,pytest.ini無疑是最為重要和常用的一個(gè),本文將深入探討pytest.ini的配置、工作原理以及實(shí)際應(yīng)用場景,幫助讀者更好地理解和使用這一強(qiáng)大的工具,需要的朋友可以參考下2025-03-03
python中time tzset()函數(shù)實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中time tzset()函數(shù)實(shí)例用法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
Windows系統(tǒng)下安裝Python的SSH模塊教程
這篇文章主要介紹了Windows系統(tǒng)下安裝Python的SSH模塊教程,本文涵蓋了pycrypto、ecdsa、paramiko、OpenSSH、SSH等模塊的安裝,需要的朋友可以參考下2015-02-02

