日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python也太好用了吧!一個plotly庫就能實現(xiàn)交互式數(shù)據(jù)可視化

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

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

plotly express簡介

plotly express是 plotly 包裝器,它允許使用更簡單的語法。

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

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

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

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

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

在本文中,我們將使用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、線圖

要在圖形上添加一個國家的疫情數(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()  

單線圖

要添加更多國家的數(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])  

多線圖

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

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

帶標記的圖形

最后,添加相關的軸標簽,設置字體大小并替換默認模板。

 
 
 
  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. )  

2、條形圖

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

 
 
 
  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、餅狀圖

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

 
 
 
  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] #時間戳  
  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()  

交互控件

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

1、范圍滑塊

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

 

 
 
 
  1. fig.update_xaxes(rangeslider_visible=True)  

2、范圍焦點

如果我們的用戶只想關注某個時間段里的某些部分呢?我們可以直接建立這些控件!

 

 
 
 
  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、自定義按鈕

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

 

 
 
 
  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ù)中,獲得一個下拉菜單,就像注釋掉一行代碼一樣簡單。在這里,你只需注釋掉“type=”buttons“就可以:

結論

Plotly express絕對是一個非常棒的數(shù)據(jù)可視化工具,它非常容易獲取,使用起來也非常像Python。在這篇文章里,我們只是簡單地描述了它所提供的功能。我鼓勵你進一步探索這個Python庫,因為它具有無限可能性!


新聞名稱:Python也太好用了吧!一個plotly庫就能實現(xiàn)交互式數(shù)據(jù)可視化
文章源于:http://www.dlmjj.cn/article/djphgph.html