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

對(duì)不起,你的PPT數(shù)據(jù)不夠直觀,你可能需要讓數(shù)據(jù)動(dòng)起來(lái)

新聞 前端
數(shù)據(jù)暴增的年代,數(shù)據(jù)科學(xué)家、分析師在被要求對(duì)數(shù)據(jù)有更深的理解與分析的同時(shí),還需要將結(jié)果有效地傳遞給他人。如何讓目標(biāo)聽(tīng)眾更直觀地理解?當(dāng)然是將數(shù)據(jù)可視化啊,而且最好是動(dòng)態(tài)可視化。

 在讀技術(shù)博客的過(guò)程中,我們會(huì)發(fā)現(xiàn)那些能夠把知識(shí)、成果講透的博主很多都會(huì)做動(dòng)態(tài)圖表。他們的圖是怎么做的?難度大嗎?這篇文章就介紹了 Python 中一種簡(jiǎn)單的動(dòng)態(tài)圖表制作方法。

对不起,你的PPT数据不够直观,你可能需要让数据动起来

數(shù)據(jù)暴增的年代,數(shù)據(jù)科學(xué)家、分析師在被要求對(duì)數(shù)據(jù)有更深的理解與分析的同時(shí),還需要將結(jié)果有效地傳遞給他人。如何讓目標(biāo)聽(tīng)眾更直觀地理解?當(dāng)然是將數(shù)據(jù)可視化啊,而且最好是動(dòng)態(tài)可視化。

本文將以線型圖、條形圖和餅圖為例,系統(tǒng)地講解如何讓你的數(shù)據(jù)圖表動(dòng)起來(lái)。

对不起,你的PPT数据不够直观,你可能需要让数据动起来

這些動(dòng)態(tài)圖表是用什么做的?

接觸過(guò)數(shù)據(jù)可視化的同學(xué)應(yīng)該對(duì) Python 里的 Matplotlib 庫(kù)并不陌生。它是一個(gè)基于 Python 的開(kāi)源數(shù)據(jù)繪圖包,僅需幾行代碼就可以幫助開(kāi)發(fā)者生成直方圖、功率譜、條形圖、散點(diǎn)圖等。這個(gè)庫(kù)里有個(gè)非常實(shí)用的擴(kuò)展包——FuncAnimation,可以讓我們的靜態(tài)圖表動(dòng)起來(lái)。

FuncAnimation 是 Matplotlib 庫(kù)中 Animation 類(lèi)的一部分,后續(xù)會(huì)展示多個(gè)示例。如果是首次接觸,你可以將這個(gè)函數(shù)簡(jiǎn)單地理解為一個(gè) While 循環(huán),不停地在 “畫(huà)布” 上重新繪制目標(biāo)數(shù)據(jù)圖。

如何使用 FuncAnimation?

這個(gè)過(guò)程始于以下兩行代碼:

  1. import matplotlib.animation as ani 
  2.  
  3. animator = ani.FuncAnimation(fig, chartfunc, interval = 100

從中我們可以看到 FuncAnimation 的幾個(gè)輸入:

  • fig 是用來(lái) 「繪制圖表」的 figure 對(duì)象;
  • chartfunc 是一個(gè)以數(shù)字為輸入的函數(shù),其含義為時(shí)間序列上的時(shí)間;
  • interval 這個(gè)更好理解,是幀之間的間隔延遲,以毫秒為單位,默認(rèn)值為 200。

這是三個(gè)關(guān)鍵輸入,當(dāng)然還有更多可選輸入,感興趣的讀者可查看原文檔,這里不再贅述。

下一步要做的就是將數(shù)據(jù)圖表參數(shù)化,從而轉(zhuǎn)換為一個(gè)函數(shù),然后將該函數(shù)時(shí)間序列中的點(diǎn)作為輸入,設(shè)置完成后就可以正式開(kāi)始了。

在開(kāi)始之前依舊需要確認(rèn)你是否對(duì)基本的數(shù)據(jù)可視化有所了解。也就是說(shuō),我們先要將數(shù)據(jù)進(jìn)行可視化處理,再進(jìn)行動(dòng)態(tài)處理。

按照以下代碼進(jìn)行基本調(diào)用。另外,這里將采用大型流行病的傳播數(shù)據(jù)作為案例數(shù)據(jù)(包括每天的死亡人數(shù))。

  1. import matplotlib.animation as ani 
  2. import matplotlib.pyplot as plt 
  3. import numpy as np 
  4. import pandas as pdurl = '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')df_interest = df.loc[ 
  6.     df['Country/Region'].isin(['United Kingdom''US''Italy''Germany']) 
  7.     & df['Province/State'].isna()]df_interest.rename( 
  8.     index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True) 
  9. df1 = df_interest.transpose()df1 = df1.drop(['Province/State''Country/Region''Lat''Long']) 
  10. df1 = df1.loc[(df1 != 0).any(1)] 
  11. df1.index = pd.to_datetime(df1.index) 

繪制三種常見(jiàn)動(dòng)態(tài)圖表

繪制動(dòng)態(tài)線型圖

如下所示,首先需要做的第一件事是定義圖的各項(xiàng),這些基礎(chǔ)項(xiàng)設(shè)定之后就會(huì)保持不變。它們包括:創(chuàng)建 figure 對(duì)象,x 標(biāo)和 y 標(biāo),設(shè)置線條顏色和 figure 邊距等:

  1. import numpy as np 
  2.  
  3. import matplotlib.pyplot as pltcolor = ['red''green''blue''orange'
  4.  
  5. fig = plt.figure() 
  6.  
  7. plt.xticks(rotation=45, ha="right", rotation_mode="anchor") #rotate the x-axis values 
  8.  
  9. plt.subplots_adjust(bottom = 0.2, top = 0.9) #ensuring the dates (on the x-axis) fit in the screen 
  10.  
  11. plt.ylabel('No of Deaths'
  12.  
  13. plt.xlabel('Dates'

接下來(lái)設(shè)置 curve 函數(shù),進(jìn)而使用 .FuncAnimation 讓它動(dòng)起來(lái):

  1. def buildmebarchart(i=int): 
  2.  
  3.     plt.legend(df1.columns) 
  4.  
  5.     p = plt.plot(df1[:i].index, df1[:i].values) #note it only returns the dataset, up to the point i 
  6.  
  7.     for i in range(0,4): 
  8.  
  9.         p[i].set_color(color[i]) #set the colour of each curveimport matplotlib.animation as ani 
  10.  
  11. animator = ani.FuncAnimation(fig, buildmebarchart, interval = 100
  12.  
  13. plt.show() 

動(dòng)態(tài)餅狀圖

对不起,你的PPT数据不够直观,你可能需要让数据动起来

可以觀察到,其代碼結(jié)構(gòu)看起來(lái)與線型圖并無(wú)太大差異,但依舊有細(xì)小的差別。

  1. import numpy as np 
  2.  
  3. import matplotlib.pyplot as pltfig,ax = plt.subplots() 
  4.  
  5. explode=[0.01,0.01,0.01,0.01] #pop out each slice from the piedef getmepie(i): 
  6.  
  7.     def absolute_value(val): #turn % back to a number 
  8.  
  9.         a  = np.round(val/100.*df1.head(i).max().sum(), 0
  10.  
  11.         return int(a) 
  12.  
  13.     ax.clear() 
  14.  
  15.     plot = df1.head(i).max().plot.pie(y=df1.columns,autopct=absolute_value, label='',explode = explode, shadow = True) 
  16.  
  17.     plot.set_title('Total Number of Deathsn' + str(df1.index[min( i, len(df1.index)-1 )].strftime('%y-%m-%d')), fontsize=12)import matplotlib.animation as ani 
  18.  
  19. animator = ani.FuncAnimation(fig, getmepie, interval = 200
  20.  
  21. plt.show() 

主要區(qū)別在于,動(dòng)態(tài)餅狀圖的代碼每次循環(huán)都會(huì)返回一組數(shù)值,但在線型圖中返回的是我們所在點(diǎn)之前的整個(gè)時(shí)間序列。返回時(shí)間序列通過(guò) df1.head(i) 來(lái)實(shí)現(xiàn),而. max()則保證了我們僅獲得最新的數(shù)據(jù),因?yàn)榱餍胁?dǎo)致死亡的總數(shù)只有兩種變化:維持現(xiàn)有數(shù)量或持續(xù)上升。

  1. df1.head(i).max() 

動(dòng)態(tài)條形圖

創(chuàng)建動(dòng)態(tài)條形圖的難度與上述兩個(gè)案例并無(wú)太大差別。在這個(gè)案例中,作者定義了水平和垂直兩種條形圖,讀者可以根據(jù)自己的實(shí)際需求來(lái)選擇圖表類(lèi)型并定義變量欄。

  1. fig = plt.figure() 
  2.  
  3. bar = ''def buildmebarchart(i=int): 
  4.  
  5.     iv = min(i, len(df1.index)-1) #the loop iterates an extra one time, which causes the dataframes to go out of bounds. This was the easiest (most lazy) way to solve this :) 
  6.  
  7.     objects = df1.max().index 
  8.  
  9.     y_pos = np.arange(len(objects)) 
  10.  
  11.     performance = df1.iloc[[iv]].values.tolist()[0
  12.  
  13.     if bar == 'vertical'
  14.  
  15.         plt.bar(y_pos, performance, align='center', color=['red''green''blue''orange']) 
  16.  
  17.         plt.xticks(y_pos, objects) 
  18.  
  19.         plt.ylabel('Deaths'
  20.  
  21.         plt.xlabel('Countries'
  22.  
  23.         plt.title('Deaths per Country n' + str(df1.index[iv].strftime('%y-%m-%d'))) 
  24.  
  25.     else
  26.  
  27.         plt.barh(y_pos, performance, align='center', color=['red''green''blue''orange']) 
  28.  
  29.         plt.yticks(y_pos, objects) 
  30.  
  31.         plt.xlabel('Deaths'
  32.  
  33.         plt.ylabel('Countries')animator = ani.FuncAnimation(fig, buildmebarchart, interval=100)plt.show() 

在制作完成后,存儲(chǔ)這些動(dòng)態(tài)圖就非常簡(jiǎn)單了,可直接使用以下代碼:

  1. animator.save(r'C:tempmyfirstAnimation.gif'

感興趣的讀者如想獲得詳細(xì)信息可參考:https://matplotlib.org/3.1.1/api/animation_api.html。

 

 

責(zé)任編輯:張燕妮 來(lái)源: 機(jī)器之心
相關(guān)推薦

2019-05-21 14:18:09

PygamePython編程語(yǔ)言

2012-09-03 09:21:51

2022-07-13 15:46:57

Python數(shù)據(jù)可視化代碼片段

2015-12-01 13:51:52

Webrtc

2021-01-18 10:36:13

移動(dòng)辦公首席信息官CIO

2021-08-02 23:19:06

微信小程序人工智能

2019-10-10 09:41:54

AI 數(shù)據(jù)人工智能

2016-11-15 15:10:07

2022-02-24 08:30:24

操作系統(tǒng)CPU程序

2009-06-19 11:18:51

Factory BeaSpring配置

2022-06-07 09:00:32

PythonAI靜態(tài)圖片

2021-01-08 08:22:25

代碼應(yīng)用程序

2020-11-16 11:50:21

Python代碼命令

2012-11-19 14:25:07

數(shù)據(jù)中心SDN

2012-11-19 16:32:16

數(shù)據(jù)中心

2024-09-26 09:38:06

2024-09-18 15:15:35

2013-05-27 15:35:18

用友UAP移動(dòng)應(yīng)用移動(dòng)平臺(tái)

2010-09-01 17:35:41

云計(jì)算

2023-12-10 20:37:48

Kafka數(shù)據(jù)庫(kù)工具
點(diǎn)贊
收藏

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