自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

掌握Python八種繪圖類型帶你深入時(shí)間序列數(shù)據(jù)分析

開發(fā) 后端
折線圖常用于展示時(shí)間序列數(shù)據(jù)的趨勢(shì)和變化,散點(diǎn)圖用于呈現(xiàn)離散數(shù)據(jù)點(diǎn)的分布。柱狀圖適合比較不同時(shí)間點(diǎn)或組之間的數(shù)據(jù),而面積圖可以突出數(shù)據(jù)點(diǎn)之間的關(guān)系。

時(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è)。

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2023-01-24 17:14:59

2022-11-03 11:32:24

數(shù)據(jù)Python方法

2021-06-24 17:55:40

Python 開發(fā)編程語(yǔ)言

2017-08-01 23:44:25

數(shù)據(jù)分析數(shù)據(jù)科學(xué)數(shù)據(jù)

2024-06-12 11:57:51

2020-07-07 14:35:41

Python數(shù)據(jù)分析命令

2017-06-28 14:54:17

大數(shù)據(jù)數(shù)據(jù)分析

2025-01-06 05:10:00

Python數(shù)據(jù)類型編程

2017-07-27 14:01:51

大數(shù)據(jù)數(shù)據(jù)分析類型模式

2025-04-27 08:35:00

Python數(shù)據(jù)分析編程

2022-09-07 15:47:21

數(shù)據(jù)分析對(duì)比分析大數(shù)據(jù)

2020-04-21 10:11:03

Python數(shù)據(jù)分析Pandas

2022-05-09 18:46:28

EOQ模型數(shù)據(jù)分析

2021-09-23 18:12:09

大數(shù)據(jù)分析預(yù)測(cè)分析

2015-08-06 14:02:31

數(shù)據(jù)分析

2022-02-21 17:35:50

漏斗模型流程數(shù)據(jù)

2019-09-24 14:36:38

數(shù)據(jù)分析思維大數(shù)據(jù)

2020-10-25 08:56:31

數(shù)據(jù)分析數(shù)據(jù)大數(shù)據(jù)

2018-07-19 05:29:37

2021-07-16 09:55:46

數(shù)據(jù)工具軟件
點(diǎn)贊
收藏

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