17
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Matplotlibで時系列データをplotする

Last updated at Posted at 2017-08-04

時系列(time series)データをプロットするメモ

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline


# 温度データ
df = pd.DataFrame({
    '時刻': pd.to_datetime(['2017-08-01 01:00:00', '2018-08-01 02:00:00', '2019-08-01 03:00:00'])
    ,'温度': [21, 22, 23]
    ,'湿度': [41, 42, 43]
})
Screen Shot 2017-08-05 at 8.28.30.png

時刻データはpandasのTimestamp型であることを確認

type(df_temp.loc[0, '時刻'])

結果
pandas._libs.tslib.Timestamp

時刻をindexにして, plt.scatterでプロットするときにX軸はdf.indexとして指定する.

df = df.set_index('時刻')
plt.scatter(df.index, df['温度'])
plt.scatter(df.index, df['湿度'])

plt.xticks(rotation=70)

plt.show()
Screen Shot 2017-08-05 at 8.36.38.png

ソースコード

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# 温度データ
df = pd.DataFrame({
    '時刻': pd.to_datetime(['2017-08-01 01:00:00', '2018-08-01 02:00:00', '2019-08-01 03:00:00'])
    ,'温度': [21, 22, 23]
    ,'湿度': [41, 42, 43]
})

df = df.set_index('時刻')
plt.scatter(df.index, df['温度'])
plt.scatter(df.index, df['湿度'])

plt.xticks(rotation=70)

plt.show()
17
24
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?