Python實(shí)現(xiàn)動態(tài)條形圖的示例詳解
關(guān)于數(shù)據(jù)可視化的模塊,之前已經(jīng)分享過很多了,小伙伴們可以到歷史文章中搜索,不過都是靜態(tài)的可視化數(shù)據(jù)展示效果。
這幾天剛剛發(fā)現(xiàn)的這款動態(tài)數(shù)據(jù)可視化模塊pynimate值得一提。
它可以將返回的pandas.DataFrame數(shù)據(jù)對象直接進(jìn)行解析,最后顯示為可視化動態(tài)數(shù)據(jù)。
https://github.com/julkaar9/pynimate
https://julkaar9.github.io/pynimate/
上述分別是pynimate模塊的GitHub源碼地址和接口API文檔地址,可以參考完成相應(yīng)的數(shù)據(jù)可視化。
目前,官方的API文檔只提供了一個條形圖的源代碼實(shí)例,可能大佬平臺太忙了沒有時間寫文檔吧!
from?matplotlib?import?pyplot?as?plt import?pandas?as?pd import?pynimate?as?nim df?=?pd.DataFrame( ????{ ????????"time":?["1960-01-01",?"1961-01-01",?"1962-01-01"], ????????"Afghanistan":?[1,?2,?3], ????????"Angola":?[2,?3,?4], ????????"Albania":?[1,?2,?5], ????????"USA":?[5,?3,?4], ????????"Argentina":?[1,?4,?5], ????} ).set_index("time") cnv?=?nim.Canvas() bar?=?nim.Barplot(df,?"%Y-%m-%d",?"2d") bar.set_time(callback=lambda?i,?datafier:?datafier.data.index[i].strftime("%b,?%Y")) cnv.add_plot(bar) cnv.animate() plt.show()
直接使用pip的方式安裝pynimate模塊,需要注意的是該模塊直接支持的是3.9以上的python版本,各個鏡像站應(yīng)該都有提供。
pip?install?pynimate pip?install?matplotlib pip?install?pandas
安裝完成之后,我們直接啟動當(dāng)前的.py模塊會出現(xiàn)下面的動態(tài)條形圖的效果。
相比其他的python可視化模塊,pynimate比較優(yōu)秀的是它可以將動態(tài)圖形的執(zhí)行過程直接保存為Gif格式的動態(tài)圖片。
cnv.save("file",?24,?"gif")
另外,該pynimate模塊作者也提供了可以通過自定義的方式去設(shè)置可視化動態(tài)圖形的方式供我們可以參考。
from?matplotlib?import?pyplot?as?plt import?numpy?as?np import?pandas?as?pd import?os dir_path?=?os.path.dirname(os.path.realpath(__file__)) import?pynimate?as?nim def?post_update(ax,?i,?datafier,?bar_attr): ????ax.spines["top"].set_visible(False) ????ax.spines["right"].set_visible(False) ????ax.spines["bottom"].set_visible(False) ????ax.spines["left"].set_visible(False) ????ax.set_facecolor("#001219") ????for?bar,?x,?y?in?zip( ????????bar_attr.top_bars, ????????bar_attr.bar_length, ????????bar_attr.bar_rank, ????): ????????ax.text( ????????????x?-?0.3, ????????????y, ????????????datafier.col_var.loc[bar,?"continent"], ????????????ha="right", ????????????color="k", ????????????size=12, ????????) df?=?pd.read_csv(dir_path?+?"/data/sample.csv").set_index("time") col?=?pd.DataFrame( ????{ ????????"columns":?["Afghanistan",?"Angola",?"Albania",?"USA",?"Argentina"], ????????"continent":?["Asia",?"Africa",?"Europe",?"N?America",?"S?America"], ????} ).set_index("columns") bar_cols?=?{ ????"Afghanistan":?"#2a9d8f", ????"Angola":?"#e9c46a", ????"Albania":?"#e76f51", ????"USA":?"#a7c957", ????"Argentina":?"#e5989b", } cnv?=?nim.Canvas(figsize=(12.8,?7.2),?facecolor="#001219") bar?=?nim.Barplot( ????df,?"%Y-%m-%d",?"3d",?post_update=post_update,?rounded_edges=True,?grid=False ) bar.add_var(col_var=col) bar.set_bar_color(bar_cols) bar.set_title("Sample?Title",?color="w",?weight=600) bar.set_xlabel("xlabel",?color="w") bar.set_time( ????callback=lambda?i,?datafier:?datafier.data.index[i].strftime("%b,?%Y"),?color="w" ) bar.set_text( ????"sum", ????callback=lambda?i,?datafier:?f"Total?:{np.round(datafier.data.iloc[i].sum(),2)}", ????size=20, ????x=0.72, ????y=0.20, ????color="w", ) bar.set_bar_annots(color="w",?size=13) bar.set_xticks(colors="w",?length=0,?labelsize=13) bar.set_yticks(colors="w",?labelsize=13) bar.set_bar_border_props( ????edge_color="black",?pad=0.1,?mutation_aspect=1,?radius=0.2,?mutation_scale=0.6 ) cnv.add_plot(bar) cnv.animate() plt.show()
上面通過自定義的方式實(shí)現(xiàn)動態(tài)條形圖效果更加炫酷,給開發(fā)者保留了更多的發(fā)揮空間,結(jié)果展示如下。
到此這篇關(guān)于Python實(shí)現(xiàn)動態(tài)條形圖的示例詳解的文章就介紹到這了,更多相關(guān)Python動態(tài)條形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)搜索本地文件信息寫入文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)搜索本地文件信息寫入文件的方法,涉及Python針對文件的遍歷及屬性操作相關(guān)技巧,需要的朋友可以參考下2016-02-02零基礎(chǔ)寫python爬蟲之使用Scrapy框架編寫爬蟲
前面的文章我們介紹了Python爬蟲框架Scrapy的安裝與配置等基本資料,本文我們就來看看如何使用Scrapy框架方便快捷的抓取一個網(wǎng)站的內(nèi)容,隨便選個小站(dmoz.org)來示例吧2014-11-11pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式示例
pytest提供了許多運(yùn)行命令以供定制化運(yùn)行某一類測試用例或者某個測試用例等,下面這篇文章主要給大家介紹了關(guān)于pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12