最初の時系列データをプロットする

Pythonで学ぶ時系列データの可視化

Thomas Vincent

Head of Data Science, Getty Images

Matplotlib ライブラリ

  • Python では、matplotlib はデータのプロットに広く使われるパッケージです
  • matplotlib のサブモジュール pyplot は、慣例として plt エイリアスでインポートされます
import matplotlib.pyplot as plt
Pythonで学ぶ時系列データの可視化

時系列データのプロット

最初の時系列データ

Pythonで学ぶ時系列データの可視化

時系列データのプロット

import matplotlib.pyplot as plt
import pandas as pd

df = df.set_index('date_column')
df.plot()
plt.show()
Pythonで学ぶ時系列データの可視化

プロットへのスタイル適用

plt.style.use('fivethirtyeight')
df.plot()
plt.show()
Pythonで学ぶ時系列データの可視化

FiveThirtyEight スタイル

fivethirtyeight スタイルの使用例

Pythonで学ぶ時系列データの可視化

Matplotlib スタイルシート

print(plt.style.available)
['seaborn-dark-palette', 'seaborn-darkgrid', 
'seaborn-dark', 'seaborn-notebook', 
'seaborn-pastel', 'seaborn-white', 
'classic', 'ggplot', 'grayscale', 
'dark_background', 'seaborn-poster', 
'seaborn-muted', 'seaborn', 'bmh', 
'seaborn-paper', 'seaborn-whitegrid', 
'seaborn-bright', 'seaborn-talk', 
'fivethirtyeight', 'seaborn-colorblind', 
'seaborn-deep', 'seaborn-ticks']

Pythonで学ぶ時系列データの可視化

ラベルによるグラフの説明

ax = df.plot(color='blue')
ax.set_xlabel('Date')
ax.set_ylabel('The values of my Y axis')
ax.set_title('The title of my plot')
plt.show()

グラフへのラベル付け

Pythonで学ぶ時系列データの可視化

図のサイズ・線幅・線種・フォントサイズ

ax = df.plot(figsize=(12, 5), fontsize=12,
             linewidth=3, linestyle='--')
ax.set_xlabel('Date', fontsize=16)
ax.set_ylabel('The values of my Y axis', fontsize=16)
ax.set_title('The title of my plot', fontsize=16)
plt.show()

Pythonで学ぶ時系列データの可視化

練習しましょう!

Pythonで学ぶ時系列データの可視化

Preparing Video For Download...