掌握Python八種繪圖類型帶你深入時間序列數(shù)據(jù)分析
時間序列數(shù)據(jù)是許多領(lǐng)域的核心,從金融市場到氣象學(xué),都需要對時間序列數(shù)據(jù)進行分析和可視化。
Python提供了豐富的庫和工具,用于處理和繪制時間序列數(shù)據(jù)。
以下8種不同的繪圖類型,在分析時間序列數(shù)據(jù)比較常用。
1、折線圖
折線圖是最常見的時間序列數(shù)據(jù)可視化類型之一。它顯示了數(shù)據(jù)隨時間的變化趨勢,通常以連續(xù)的折線表示。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(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、散點圖
散點圖用于表示數(shù)據(jù)點的分布和關(guān)系,適合展示時間序列數(shù)據(jù)中的離散觀測。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(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('散點圖')
plt.show()
3、柱狀圖
柱狀圖適用于展示時間序列數(shù)據(jù)的分組或分類,通常用于比較不同時間點或不同組之間的數(shù)據(jù)。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(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ù)據(jù)的趨勢和數(shù)據(jù)點之間的關(guān)系。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(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ù)據(jù)的統(tǒng)計分布,包括中位數(shù)、四分位數(shù)和異常值。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(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ù)據(jù)的占比和相對比例,適用于表示各部分在整體中的貢獻。
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ù)據(jù)的關(guān)系和相似性,通常用于呈現(xiàn)多維數(shù)據(jù)集。
import seaborn as sns
import pandas as pd
# 創(chuàng)建時間序列數(shù)據(jù)
data = {'時間': 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、雷達圖
雷達圖用于展示多個維度的時間序列數(shù)據(jù),比較不同類別或時間點的數(shù)據(jù)分布。
import matplotlib.pyplot as plt
import pandas as pd
# 創(chuàng)建時間序列數(shù)據(jù)
data = {'時間': 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('雷達圖')
plt.show()
總結(jié)
Python進行時間序列分析的8種常見繪圖類型,每種類型都具有獨特的用途和適用場景。
折線圖常用于展示時間序列數(shù)據(jù)的趨勢和變化,散點圖用于呈現(xiàn)離散數(shù)據(jù)點的分布。柱狀圖適合比較不同時間點或組之間的數(shù)據(jù),而面積圖可以突出數(shù)據(jù)點之間的關(guān)系。箱線圖有助于了解數(shù)據(jù)的分布和離群值。餅圖適用于顯示數(shù)據(jù)占比,熱圖用于呈現(xiàn)多維數(shù)據(jù)的關(guān)系,而雷達圖展示多個維度的時間序列數(shù)據(jù)。
通過運用這些繪圖技巧,可以提高對時間序列數(shù)據(jù)的洞察力,發(fā)現(xiàn)隱藏在數(shù)據(jù)中的信息,從而做出更明智的決策和預(yù)測。