兩個(gè)簡(jiǎn)單的代碼片段讓你的圖表動(dòng)起來(lái)
本篇文章整列了2個(gè)簡(jiǎn)單的代碼片段,可以讓你的圖表動(dòng)起來(lái)。
動(dòng)畫(huà)
Python中有許多用于繪制圖形的庫(kù)。Matplotlib, Seaborn, Bokeh, Plotly等等。
但是我們繪圖的目的是要向聽(tīng)眾和要傳遞的信息。如果你的圖能夠動(dòng)起來(lái)那么他們肯定會(huì)讓聽(tīng)眾在看第一眼的時(shí)候就印象深刻。但是并不是每個(gè)圖形或數(shù)據(jù)集都適合動(dòng)畫(huà)。一般情況下,動(dòng)畫(huà)對(duì)時(shí)間序列來(lái)說(shuō)非常有效。例如,根據(jù)時(shí)間變化進(jìn)行數(shù)據(jù)的對(duì)比。
Plotly Express
Plotly Express,可以直接為我們創(chuàng)建動(dòng)態(tài)圖表:
import plotly.express as px
import pandas as pd
import numpy as np
讓我們?cè)跀?shù)據(jù)集中創(chuàng)建一些值。
df = pd.DataFrame( {'week': np.random.randint(1,21, size=200),
'P1': np.random.randint(10,220, size=200),
'P2': np.random.randint(15,200, size=200),
'P3': np.random.randint(10,490, size=200),
'P4': np.random.randint(10,980, size=200) } )
df = pd.DataFrame( {'week': np.random.randint(1,21, size=200),
'P1': np.random.randint(10,220, size=200),
'P2': np.random.randint(15,200, size=200),
'P3': np.random.randint(10,490, size=200),
'P4': np.random.randint(10,980, size=200) } )
現(xiàn)在我們可以繪制一個(gè)動(dòng)畫(huà)圖來(lái)查看產(chǎn)品按周的變化情況。
創(chuàng)建散點(diǎn)圖動(dòng)畫(huà)也同樣簡(jiǎn)單。
fig = px.scatter(df, x="week", y="sales", animation_frame="week", animation_group="product", size="sales", color="product", hover_name="product", range_x=[0,20], range_y=[0,800])
fig.update_layout(height=600, width=1000)
gif庫(kù)
如果你向我一樣是matplot和seaborn的粉絲,并且不太喜歡用Plotly的話,那么可以試試這個(gè)庫(kù)。這個(gè)庫(kù)的作用是創(chuàng)建一系列繪圖,并將它們放在一個(gè)幀序列中并創(chuàng)建一個(gè)動(dòng)態(tài)的gif圖。
首先,還是獲取一些用于繪圖的時(shí)間序列數(shù)據(jù)。
import seaborn as sns
df = sns.load_dataset('flights')
接下來(lái)創(chuàng)建一個(gè)函數(shù),該函數(shù)將為每個(gè)觀察創(chuàng)建一個(gè)繪圖。
@gif.frame
def plot_flights(df, i):
df = df.copy()
# Get the year for the plot title
yr = df['year'][i]
# Force X axis to be entirely plotted at once
df.iloc[i:] = np.nan
#Plot
ax = df.plot(x='month', y= 'passengers', legend=False,
style="o-", figsize=(20,10))
ax.set_title(f"Air Passengers {yr}", size=20)
ax.set_xlabel("Months")
ax.set_ylabel("Number of Passengers")
@gif.frame是GIF庫(kù)用來(lái)創(chuàng)建幀序列的裝飾器。
df.iloc[i:] = np.nan將把所有未來(lái)的數(shù)據(jù)轉(zhuǎn)換到NA。這是一種每次只繪制一個(gè)值的編程方式(i=0所有都為nan, i=1,只繪制索引0,i=2,只繪制0和1…),通過(guò)這種方法我們可以端到端繪制X軸,因?yàn)樵趧?dòng)畫(huà)期間是不會(huì)改變的。這樣也可以保持圖表的大小不變,使其更容易觀看。
現(xiàn)在我們使用函數(shù)創(chuàng)建一個(gè)循環(huán)來(lái)創(chuàng)建幀。
frames = []
for i in range(df.shape[0]):
frame = plot_flights(df, i)
frames.append(frame)
最后,保存生成的GIF圖像。
gif.save(frames, 'gif_example.gif', duration=180)
看,是不是很簡(jiǎn)單
最后總結(jié)
動(dòng)畫(huà)圖是一個(gè)很有影響力的展示方法,但是并不是所有的圖都適合動(dòng)畫(huà)化。我們應(yīng)該根據(jù)實(shí)際的情況來(lái)選擇是否需要?jiǎng)?chuàng)建動(dòng)畫(huà)圖,因?yàn)閯?dòng)畫(huà)圖并不是深入分析的最佳選擇他只是在視覺(jué)上有一些更大的沖擊,所以當(dāng)你需要觀察、比較和理解時(shí)也許靜態(tài)圖是更好的選擇。
要?jiǎng)?chuàng)建動(dòng)圖,我建議您使用gif庫(kù),因?yàn)閷?duì)于這種圖形類(lèi)型,它比plotly更簡(jiǎn)單(因?yàn)槲覀€(gè)人更喜歡seaborn,哈)。