掌握Python八種繪圖類型帶你深入時(shí)間序列數(shù)據(jù)分析
時(shí)間序列數(shù)據(jù)是許多領(lǐng)域的核心,從金融市場(chǎng)到氣象學(xué),都需要對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行分析和可視化。
Python提供了豐富的庫(kù)和工具,用于處理和繪制時(shí)間序列數(shù)據(jù)。
以下8種不同的繪圖類型,在分析時(shí)間序列數(shù)據(jù)比較常用。
1、折線圖
折線圖是最常見的時(shí)間序列數(shù)據(jù)可視化類型之一。它顯示了數(shù)據(jù)隨時(shí)間的變化趨勢(shì),通常以連續(xù)的折線表示。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'日期': pd.date_range(start='2023-01-01', periods=30, freq='D'),
'數(shù)值': [10, 15, 13, 12, 18, 20, 22, 25, 28, 30, 35, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4]}
df = pd.DataFrame(data)
plt.plot(df['日期'], df['數(shù)值'])
plt.xlabel('日期')
plt.ylabel('數(shù)值')
plt.title('折線圖')
plt.show()
2、散點(diǎn)圖
散點(diǎn)圖用于表示數(shù)據(jù)點(diǎn)的分布和關(guān)系,適合展示時(shí)間序列數(shù)據(jù)中的離散觀測(cè)。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'日期': pd.date_range(start='2023-01-01', periods=30, freq='D'),
'數(shù)值': [10, 15, 13, 12, 18, 20, 22, 25, 28, 30, 35, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4]}
df = pd.DataFrame(data)
plt.scatter(df['日期'], df['數(shù)值'])
plt.xlabel('日期')
plt.ylabel('數(shù)值')
plt.title('散點(diǎn)圖')
plt.show()
3、柱狀圖
柱狀圖適用于展示時(shí)間序列數(shù)據(jù)的分組或分類,通常用于比較不同時(shí)間點(diǎn)或不同組之間的數(shù)據(jù)。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'日期': pd.date_range(start='2023-01-01', periods=5, freq='D'),
'數(shù)值1': [10, 15, 13, 12, 18],
'數(shù)值2': [5, 8, 7, 6, 10]}
df = pd.DataFrame(data)
df.set_index('日期', inplace=True)
df.plot(kind='bar')
plt.xlabel('日期')
plt.ylabel('數(shù)值')
plt.title('柱狀圖')
plt.show()
4、面積圖
面積圖是折線圖的一種變體,用于顯示時(shí)間序列數(shù)據(jù)的趨勢(shì)和數(shù)據(jù)點(diǎn)之間的關(guān)系。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'日期': pd.date_range(start='2023-01-01', periods=30, freq='D'),
'數(shù)值1': [10, 15, 13, 12, 18, 20, 22, 25, 28, 30, 35, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4],
'數(shù)值2': [5, 8, 7, 6, 10, 12, 15, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 35, 30, 25, 20, 15, 10, 5, 4, 3, 2]}
df = pd.DataFrame(data)
plt.fill_between(df['日期'], df['數(shù)值1'], df['數(shù)值2'], color='lightblue')
plt.xlabel('日期')
plt.ylabel('數(shù)值')
plt.title('面積圖')
plt.show()
5、箱線圖
箱線圖用于顯示時(shí)間序列數(shù)據(jù)的統(tǒng)計(jì)分布,包括中位數(shù)、四分位數(shù)和異常值。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'日期': pd.date_range(start='2023-01-01', periods=30, freq='D'),
'數(shù)值': [10, 15, 13, 12, 18, 20, 22, 25, 28, 30, 35, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4]}
df = pd.DataFrame(data)
plt.boxplot(df['數(shù)值'])
plt.xticks([1], ['數(shù)值'])
plt.title('箱線圖')
plt.show()
6、餅圖
餅圖用于顯示時(shí)間序列數(shù)據(jù)的占比和相對(duì)比例,適用于表示各部分在整體中的貢獻(xiàn)。
import matplotlib.pyplot as plt
# 創(chuàng)建數(shù)據(jù)
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.title('餅圖')
plt.show()
7、熱圖
熱圖用于可視化時(shí)間序列數(shù)據(jù)的關(guān)系和相似性,通常用于呈現(xiàn)多維數(shù)據(jù)集。
import seaborn as sns
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'時(shí)間': pd.date_range(start='2023-01-01', periods=10, freq='D'),
'特征1': [3, 1, 4, 2, 6, 8, 7, 5, 9, 10],
'特征2': [7, 8, 6, 9, 5, 4, 2, 3, 1, 10]}
df = pd.DataFrame(data)
corr_matrix = df.corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('熱圖')
plt.show()
8、雷達(dá)圖
雷達(dá)圖用于展示多個(gè)維度的時(shí)間序列數(shù)據(jù),比較不同類別或時(shí)間點(diǎn)的數(shù)據(jù)分布。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時(shí)間序列數(shù)據(jù)
data = {'時(shí)間': pd.date_range(start='2023-01-01', periods=1, freq='D'),
'維度1': [3],
'維度2': [7],
'維度3': [5],
'維度4': [9],
'維度5': [6]}
df = pd.DataFrame(data)
categories = list(df.columns[2:])
values = df.iloc[:, 2:].values[0]
fig, ax = plt.subplots(figsize=(6, 6))
angles = [n / float(len(categories)) * 2 * 3.14159265359 for n in range(len(categories))]
angles += angles[:1]
plt.polar(angles, values)
plt.fill(angles, values, 'b', alpha=0.1)
plt.xticks(angles[:-1], categories)
plt.title('雷達(dá)圖')
plt.show()
總結(jié)
Python進(jìn)行時(shí)間序列分析的8種常見繪圖類型,每種類型都具有獨(dú)特的用途和適用場(chǎng)景。
折線圖常用于展示時(shí)間序列數(shù)據(jù)的趨勢(shì)和變化,散點(diǎn)圖用于呈現(xiàn)離散數(shù)據(jù)點(diǎn)的分布。柱狀圖適合比較不同時(shí)間點(diǎn)或組之間的數(shù)據(jù),而面積圖可以突出數(shù)據(jù)點(diǎn)之間的關(guān)系。箱線圖有助于了解數(shù)據(jù)的分布和離群值。餅圖適用于顯示數(shù)據(jù)占比,熱圖用于呈現(xiàn)多維數(shù)據(jù)的關(guān)系,而雷達(dá)圖展示多個(gè)維度的時(shí)間序列數(shù)據(jù)。
通過(guò)運(yùn)用這些繪圖技巧,可以提高對(duì)時(shí)間序列數(shù)據(jù)的洞察力,發(fā)現(xiàn)隱藏在數(shù)據(jù)中的信息,從而做出更明智的決策和預(yù)測(cè)。