偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

新聞 前端
在本文中,我們將學(xué)習(xí)如何在Python中創(chuàng)建交互式可視化。我們將從僅僅以不同格式繪制數(shù)據(jù)開始,然后再探索添加更多交互式控件。

 在本文中,我們將學(xué)習(xí)如何在Python中創(chuàng)建交互式可視化。我們將從僅僅以不同格式繪制數(shù)據(jù)開始,然后再探索添加更多交互式控件。

今天,我們將學(xué)習(xí)如何使用Plotly express。Plotly允許用戶在肉眼可見的可視化界面上進(jìn)行數(shù)據(jù)交互,并且與Web內(nèi)容集成起來要容易得多。

plotly express簡(jiǎn)介

plotly express是 plotly 包裝器,它允許使用更簡(jiǎn)單的語法。

受Seaborn和ggplot2的啟發(fā),它經(jīng)過專門設(shè)計(jì),具有簡(jiǎn)潔、一致且易于學(xué)習(xí)的API:只需一次導(dǎo)入,你就可以在一個(gè)函數(shù)調(diào)用中創(chuàng)建豐富的交互式圖,包括刻面、地圖、動(dòng)畫和趨勢(shì)線。

如果你想了解更多信息,可訪問Plotly的官方文檔:
https://medium.com/plotly/introducing-plotly-express-808df010143d

只需要兩行代碼,你就可以擁有一個(gè)漂亮的交互式圖表,非常簡(jiǎn)單:

  1. import plotly.express as px  
  2. fig = px.line(x='x data set', y= 'y data set')  
  3. fig.show()  

數(shù)據(jù)來源及準(zhǔn)備

在本文中,我們將使用COVID-19數(shù)據(jù)集。

我們將使用以下代碼來獲取和格式化數(shù)據(jù):

  1. import plotly.express as px  
  2. import numpy as np  
  3. import pandas as pd  
  4. url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'  
  5. df = pd.read_csv(url, delimiter=',', header='infer')  
  6. df_interest = df.loc[  
  7. df['Country/Region'].isin(['United Kingdom''US''Italy''Brazil''India'])  
  8. & df['Province/State'].isna()]  
  9. df_interest.rename(  
  10. index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True)  
  11. df1 = df_interest.transpose()  
  12. df1 = df1.drop(['Province/State''Country/Region''Lat''Long'])  
  13. df1 = df1.loc[(df1 != 0).any(1)]  
  14. df1.index = pd.to_datetime(df1.index)  
  15. df1 = df1.diff() #數(shù)據(jù)每日變化  

創(chuàng)建圖表

1、線圖

要在圖形上添加一個(gè)國家的疫情數(shù)據(jù)可視化,我們只需要兩行代碼:

  1. fig = px.line(x=df1.index, y= df1[df1.columns[0]],title = 'Daily Deaths due to COVID-19', name = df1.columns[0])  
  2. fig.show()  

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

單線圖

要添加更多國家的數(shù)據(jù),我們需要.add_scatter()屬性。通過使用循環(huán),我們可以添加所有范圍內(nèi)的國家。

  1. fig = px.line()  
  2. for i,n in enumerate(df1.columns):  
  3. fig.add_scatter(x=df1.index, y= df1[df1.columns[i]], name= df1.columns[i])  

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

多線圖

最后,我們可以考慮在圖中添加更多的細(xì)節(jié),個(gè)人喜歡在圖中突出顯示不同的數(shù)據(jù)點(diǎn)。

  1. fig.update_traces(mode='markers+lines')  

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

帶標(biāo)記的圖形

最后,添加相關(guān)的軸標(biāo)簽,設(shè)置字體大小并替換默認(rèn)模板。

  1. fig.update_layout(  
  2. title = 'Daily Deaths due to COVID-19'  
  3. ,xaxis_title = 'Dates'  
  4. ,yaxis_title = 'Number of Deaths'  
  5. ,font = dict(size = 25)  
  6. ,template = 'plotly_dark' #"plotly""plotly_white""plotly_dark""ggplot2""seaborn""simple_white""none"  
  7. )  
Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

2、條形圖

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

正如我們之前看到的,條形圖可以很快就可以組合起來:

  1. fig = px.bar(x=df1.index, y= df1[df1.columns[0]])  
  2. for i,n in enumerate(df1.columns):  
  3. fig.add_bar(x=df1.index, y= df1[df1.columns[i]], name= df1.columns[i])  
  4. fig.update_layout(  
  5. title = 'Daily Deaths due to COVID-19'  
  6. ,xaxis_title = 'Dates'  
  7. ,yaxis_title = 'Number of Deaths'  
  8. ,font = dict(size = 25)  
  9. ,template = 'plotly_dark' #"plotly""plotly_white""plotly_dark""ggplot2""seaborn""simple_white""none"  
  10. )  
  11. fig.show()  

3、餅狀圖

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

和以前一樣,唯一的區(qū)別是我們只顯示時(shí)間序列中的最新一天。

  1. df1 = df1.tail(1).transpose()  
  2. fig = px.pie(df1, values = str(df1.columns[0]), names = df1.index)  
  3. fig.update_traces(textposition='inside', textinfo = 'percent+label')  
  4. ddate = str(df1.columns[0])[:10] #時(shí)間戳  
  5. fig.update_layout(  
  6. title = f'Deaths on {ddate} due to COVID-19'  
  7. ,xaxis_title = 'Dates'  
  8. ,yaxis_title = 'Number of Deaths'  
  9. ,font = dict(size = 25)  
  10. ,template = 'seaborn' #"plotly""plotly_white""plotly_dark""ggplot2""seaborn""simple_white""none"  
  11. )  
  12. fig.show()  

交互控件

通過上文,我們知道了如何快速地將不同類型的可視化組合在一起,接下來我們用交互控件來增強(qiáng)數(shù)據(jù)的可視化。

1、范圍滑塊

首先,通過下面的一行代碼來添加一個(gè)范圍滑塊,這是一個(gè)很好用的控件,讓用戶看到自己控制想看的特定部分。

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化
 
  1. fig.update_xaxes(rangeslider_visible=True)  

2、范圍焦點(diǎn)

如果我們的用戶只想關(guān)注某個(gè)時(shí)間段里的某些部分呢?我們可以直接建立這些控件!

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化
 
  1.  fig.update_xaxes(  
  2. rangeslider_visible=True,  
  3. rangeselector=dict(  
  4. buttons=list([  
  5. dict(count=7, label="1w", step="day", stepmode="backward"),  
  6. dict(count=1, label="1m", step="month", stepmode="backward"),  
  7. dict(count=2, label="2m", step="month", stepmode="backward"),  
  8. dict(step="all")  
  9. ]),  
  10. font = dict( color='#008000', size = 11),  
  11. )  
  12. )  

3、自定義按鈕

在體驗(yàn)了上面的范圍焦點(diǎn)功能后,我們可以很容易想象到如何構(gòu)建自定義按鈕。Plotly express 以一種簡(jiǎn)單的方式滿足了這一需求。讓我們看看定制按鈕,把重點(diǎn)放在個(gè)別國家上。

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化
 
  1. fig.update_layout(  
  2. updatemenus=[  
  3. dict(  
  4. type="buttons",  
  5. direction="right",  
  6. active=0,  
  7. x=0.5,  
  8. y=1.03,  
  9. buttons=list([  
  10. dict(label=df1.columns[0],  
  11. method="update",  
  12. args=[ {"visible": [True, False, False, False, False]},  
  13. {'showlegend' : True}  
  14. ]),  
  15. dict(label=df1.columns[1],  
  16. method="update",  
  17. args=[ {"visible": [False, True, False, False, False]},  
  18. {'showlegend' : True}  
  19. ]),  
  20. dict(label=df1.columns[2],  
  21. method="update",  
  22. args=[ {"visible": [False, False, True, False, False]},  
  23. {'showlegend' : True}  
  24. ]),  
  25. dict(label=df1.columns[3],  
  26. method="update",  
  27. args=[ {"visible": [False, False, False, True, False]},  
  28. {'showlegend' : True}  
  29. ]),  
  30. dict(label=df1.columns[4],  
  31. method="update",  
  32. args=[ {"visible": [False, False, False, False, True]},  
  33. {'showlegend' : True}  
  34. ]),  
  35. dict(label='All',  
  36. method="update",  
  37. args=[ {"visible": [True, True, True, True, True]},  
  38. {'showlegend' : True}  
  39. ]),  
  40. ]),  
  41. )  
  42. ]  
  43. )  

4、下拉式菜單

如果你想在可視化數(shù)據(jù)中,獲得一個(gè)下拉菜單,就像注釋掉一行代碼一樣簡(jiǎn)單。在這里,你只需注釋掉“type=”buttons“就可以:

Python也太好用了吧!一個(gè)plotly庫就能實(shí)現(xiàn)交互式數(shù)據(jù)可視化

結(jié)論

Plotly express絕對(duì)是一個(gè)非常棒的數(shù)據(jù)可視化工具,它非常容易獲取,使用起來也非常像Python。在這篇文章里,我們只是簡(jiǎn)單地描述了它所提供的功能。我鼓勵(lì)你進(jìn)一步探索這個(gè)Python庫,因?yàn)樗哂袩o限可能性!

 

責(zé)任編輯:張燕妮 來源: 今日頭條
相關(guān)推薦

2021-06-09 11:26:37

BokehPython可視化

2021-08-11 09:33:15

Vue 技巧 開發(fā)工具

2011-06-13 18:54:12

2024-08-02 10:30:39

StreamlitPython庫數(shù)據(jù)驅(qū)動(dòng)

2023-12-18 15:02:00

PyechartsPython數(shù)據(jù)可視化工具

2024-12-13 16:01:35

2015-10-14 17:59:53

Google數(shù)據(jù)探索交互開發(fā)

2024-04-01 11:53:42

PlotlyPython數(shù)據(jù)可視化

2022-08-26 09:15:58

Python可視化plotly

2020-12-31 10:29:05

數(shù)據(jù)可視化可視化工具編碼

2022-05-31 09:42:49

工具編輯器

2023-09-19 15:44:03

Python數(shù)據(jù)可視化

2020-12-20 17:40:04

機(jī)器學(xué)習(xí)可視化網(wǎng)站算法

2020-12-11 08:00:00

數(shù)據(jù)可視化工具大數(shù)據(jù)

2017-01-05 15:06:23

2020-04-06 20:47:42

FishShellLinux

2020-06-29 15:40:53

PlotlyPython數(shù)據(jù)可視化

2025-04-01 08:30:00

Plotly數(shù)據(jù)可視化數(shù)據(jù)分析

2017-06-19 08:30:35

大數(shù)據(jù)數(shù)據(jù)可視化報(bào)表

2022-09-06 10:52:04

正則庫HumrePython
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)