优雅的 Python 动态图工具
生活随笔
收集整理的這篇文章主要介紹了
优雅的 Python 动态图工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者 |?pythonic生物人
來源 |?pythonic生物人
數據動態圖怎么做,效果圖,
多子圖聯動競賽圖安裝
pip?install?pandas_alive#或者 conda?install?pandas_alive?-c?conda-forge玩起來
支持數據數據格式如下,
使用方法類似pandas。
動態地圖
結合geopandas,
動態水平bar
import?pandas?as?pd import?pandas_alive import?matplotlib.pyplot?as?pltplt.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_alivemulti_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的多子圖繪制,實現各種個性化動圖,核心代碼如下,
往期回顧
太卷了!AI 高數考試正確率81%
數據分析你選Pandas還是選SQL?
2D變身3D,來看英偉達的AI“新”魔法!
如何用 Python 實現景區安防系統?
分享 點收藏 點點贊 點在看總結
以上是生活随笔為你收集整理的优雅的 Python 动态图工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 耳穴诊疗长期跟踪0524
- 下一篇: 每日练琴菜单 ---非常有效的古筝学习方