最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)動(dòng)態(tài)條形圖的示例詳解

 更新時(shí)間:2023年03月22日 10:18:28   作者:Python 集中營  
這篇文章主要為大家詳細(xì)介紹了如何利用Python中的pynimate模塊實(shí)現(xiàn)動(dòng)態(tài)條形圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

關(guān)于數(shù)據(jù)可視化的模塊,之前已經(jīng)分享過很多了,小伙伴們可以到歷史文章中搜索,不過都是靜態(tài)的可視化數(shù)據(jù)展示效果。

這幾天剛剛發(fā)現(xiàn)的這款動(dòng)態(tài)數(shù)據(jù)可視化模塊pynimate值得一提。

它可以將返回的pandas.DataFrame數(shù)據(jù)對象直接進(jìn)行解析,最后顯示為可視化動(dòng)態(tài)數(shù)據(jù)。

https://github.com/julkaar9/pynimate

https://julkaar9.github.io/pynimate/

上述分別是pynimate模塊的GitHub源碼地址和接口API文檔地址,可以參考完成相應(yīng)的數(shù)據(jù)可視化。

目前,官方的API文檔只提供了一個(gè)條形圖的源代碼實(shí)例,可能大佬平臺太忙了沒有時(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版本,各個(gè)鏡像站應(yīng)該都有提供。

pip?install?pynimate

pip?install?matplotlib

pip?install?pandas

安裝完成之后,我們直接啟動(dòng)當(dāng)前的.py模塊會(huì)出現(xiàn)下面的動(dòng)態(tài)條形圖的效果。

相比其他的python可視化模塊,pynimate比較優(yōu)秀的是它可以將動(dòng)態(tài)圖形的執(zhí)行過程直接保存為Gif格式的動(dòng)態(tài)圖片。

cnv.save("file",?24,?"gif")

另外,該pynimate模塊作者也提供了可以通過自定義的方式去設(shè)置可視化動(dòng)態(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)動(dòng)態(tài)條形圖效果更加炫酷,給開發(fā)者保留了更多的發(fā)揮空間,結(jié)果展示如下。

到此這篇關(guān)于Python實(shí)現(xiàn)動(dòng)態(tài)條形圖的示例詳解的文章就介紹到這了,更多相關(guān)Python動(dòng)態(tài)條形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

华容县| 奉贤区| 夏津县| 黄大仙区| 鄂托克旗| 九寨沟县| 孝昌县| 安乡县| 昆山市| 建湖县| 北票市| 墨竹工卡县| 北川| 乌兰县| 高雄市| 龙口市| 宿州市| 林甸县| 达孜县| 丹巴县| 东乡县| 长垣县| 左云县| 图片| 黔南| 凤山市| 韶山市| 杭锦旗| 金阳县| 花莲县| 徐汇区| 城口县| 太白县| 舟山市| 黄骅市| 宜昌市| 图们市| 子洲县| 吉林省| 阜南县| 长武县|