Help us understand the problem. What is going on with this article?

matplotlibで時系列データをプロットする際の軸の設定+おまけ

前略

時系列データをmatplotlibでプロットするとき、軸のフォーマット調整で結構苦労します。サンプルを残しておきます。

手順

時刻の列を折れ線グラフのx軸に使用する

折れ線グラフの場合、時系列の列をそのままx軸に使用できます。ただしフォーマットがmatplotlibが自動で調節して、意図しない表示になることがあります。LocatorとFormatterで軸の表示を設定します。

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import io

# データ読み込み
data = """date,val_1,val_2
2018-11-01 09:00:00,65,d g
2018-11-01 09:01:00,26,e h
2018-11-01 09:02:00,47,h w
2018-11-01 09:03:00,20,k d
2018-11-01 09:04:00,65,8 d
2018-11-01 09:05:00,4,l d
2018-11-01 09:06:00,31,w d
2018-11-01 09:07:00,21,s s
2018-11-01 09:08:00,98,a b
2018-11-01 09:09:00,48,f f
2018-11-01 09:10:00,18,g 4
2018-11-01 09:11:00,86,s f"""
df = pd.read_csv(io.StringIO(data), parse_dates=[0])

# グラフ作成
plt.figure(figsize=(8,4))
plt.plot(df['date'], df['val_1'])

# ロケータで刻み幅を設定
xloc = mpl.dates.MinuteLocator(byminute=range(0,60,1))
plt.gca().xaxis.set_major_locator(xloc)

# 時刻のフォーマットを設定
xfmt = mpl.dates.DateFormatter("%H:%M")
plt.gca().xaxis.set_major_formatter(xfmt)

plt.show()

image.png

時刻の列を棒グラフのx軸に使用する

plt.barの第一引数は棒グラフの棒の左端座標を表します。時系列データを直接使用すると座標がおかしくなってしまいます。

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import io

%matplotlib inline


data = """date,val_1,val_2
2018-11-01 09:00:00,65,d g
2018-11-01 09:01:00,26,e h
2018-11-01 09:02:00,47,h w
2018-11-01 09:03:00,20,k d
2018-11-01 09:04:00,65,8 d
2018-11-01 09:05:00,4,l d
2018-11-01 09:06:00,31,w d
2018-11-01 09:07:00,21,s s
2018-11-01 09:08:00,98,a b
2018-11-01 09:09:00,48,f f
2018-11-01 09:10:00,18,g 4
2018-11-01 09:11:00,86,s f"""


df = pd.read_csv(io.StringIO(data), parse_dates=[0])
x = range(len(df['date']))

plt.figure(figsize=(8,4))
plt.bar(df['date'], df['val_1'])

image.png

そこでx軸を整数値にとって、あとから時刻目盛りを時刻文字列で置き換えます。

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import io

%matplotlib inline


data = """date,val_1,val_2
2018-11-01 09:00:00,65,d g
2018-11-01 09:01:00,26,e h
2018-11-01 09:02:00,47,h w
2018-11-01 09:03:00,20,k d
2018-11-01 09:04:00,65,8 d
2018-11-01 09:05:00,4,l d
2018-11-01 09:06:00,31,w d
2018-11-01 09:07:00,21,s s
2018-11-01 09:08:00,98,a b
2018-11-01 09:09:00,48,f f
2018-11-01 09:10:00,18,g 4
2018-11-01 09:11:00,86,s f"""


df = pd.read_csv(io.StringIO(data), parse_dates=[0])
x = range(len(df['date']))

plt.figure(figsize=(8,4))
plt.bar(x, df['val_1'])

xloc = mpl.dates.MinuteLocator(byminute=range(0,60,1))
xfmt = mpl.dates.DateFormatter("%H:%M")
plt.gca().xaxis.set_major_locator(xloc)
plt.gca().xaxis.set_major_formatter(xfmt)

plt.xticks(x, [pd.to_datetime(str(date)).strftime("%H:%M") for date in df['date']], rotation=45)
plt.show()

image.png

草々

matplotlibの棒グラフは使いにくいですねえ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした