質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

2回答

2462閲覧

時系列分析で、0.001秒単位でグラフを表示したい【Python3】

Taiga_s

総合スコア8

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2019/10/16 09:54

0.001秒単位の時系列分析グラフを表示したい

【質問】Python3.7の時系列分析で0.001秒単位でグラフを表示させることはできるのでしょうか?

初心者です。
加速度を高速で取得したCSVデータの、傾向を見たいため
jupyter notebook(Python3.7)で時系列分析を利用しようと考えました。

output1.csv 〜 output10.csv という、1つのcsvを10分割したcsvをつなげてグラフ化させ、
その傾向を時系列分析を利用したグラフを表示させようというコードを打ってみました。

ですが、グラフを表示させてみると、
1秒毎での加速度値みたいな変なグラフが表示されました。

(0.001秒単位毎の加速度の動きを表したグラフを表示したいです)

csvデータ(output1.csvの冒頭)と、グラフは以下のようになります。
csvデータ(0922gFz)
csvをグラフ表示

(さらに、結果に
赤いバックカラーに大量の文字が表示されました。
(エラー??))

時系列分析で0.001秒単位でグラフを表示させることは可能でしょうか??
もしやり方を知っている方いらっしゃればご教授いただけると幸いです。
よろしくお願い申し上げます。

以下、試したコードと、結果です。

該当のソースコード

import statsmodels.api as sm import pandas as pd import matplotlib.pyplot as plt import numpy as np plt.subplots_adjust(hspace=0.3) plt.figure(figsize=(15, 9)) num_figure = 10 for i in range(1, num_figure+1): df = pd.read_csv('output{}.csv'.format(i)) gFz = pd.Series(df['gFz'], dtype='float') gFz.index = pd.to_datetime(df['time'], format='%S') res = sm.tsa.seasonal_decompose(gFz, freq=5) plt.subplot(411) plt.plot(gFz, lw=.6, c='darkblue') plt.title('observed') plt.subplot(412) plt.plot(res.trend, lw=.6, c='indianred') plt.title('trend') plt.subplot(413) plt.plot(res.seasonal, lw=.6, c='indianred') plt.title('seasonal') plt.subplot(414) plt.plot(res.resid, lw=.6, c='indianred') plt.title('residual')

イメージ説明

このように、滑らかではなく、カクカクしたグラフになってしまいます…

また、以下の文章が赤のバックカラーで表示されました。(問題はあるのしょうか?)

/Users/username/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:11: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. # This is added back by InteractiveShellApp.init_path() /Users/username/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:15: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. from ipykernel import kernelapp as app /Users/username/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:19: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. /Users/usernameF/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:23: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

試したこと

Pythonで時系列分析の練習(6)トレンド、季節性、残差に分解
Pandasで時間や日付データに変換するto_datetime関数の使い方
Python実務データ分析でよく使う手法(分析手法編)

上記3つのサイトを参考にコードを書きました。

res = sm.tsa.seasonal_decompose(gFz, freq=5)
のfreq(周期)の意味はまだいまいちよくわかってないです…

OS環境

macOS Catalina 10.15

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

plt.plot()内に、df['time']を追加することで解決致しました。

例えば、

plt.plot(res.trend, lw=.6, c='indianred')

としていたのを、

plt.plot(df['time'], res.trend, lw=.6, c='indianred')

とすることで、滑らかなグラフを表示させることができました!!

該当のソースコード

Python3

1import statsmodels.api as sm 2import pandas as pd 3import matplotlib.pyplot as plt 4import numpy as np 5 6plt.subplots_adjust(hspace=0.8) 7plt.figure(figsize=(15, 9)) 8 9num_figure = 10 # 描画したいcsvファイルの数 10for i in range(1, num_figure+1): 11 df = pd.read_csv('output{}.csv'.format(i)) 12 gFz = pd.Series(df['gFz'], dtype = 'float') 13 gFz.index = pd.to_datetime(df['time'], format='%S') 14 res = sm.tsa.seasonal_decompose(gFz, freq=10) 15 16 plt.subplot(411) 17 plt.plot(df['time'], gFz, lw=.6, c='darkblue') 18 plt.title('observed') 19 20 plt.subplot(412) 21 plt.plot(df['time'], res.trend, lw=.6, c='indianred') 22 plt.title('trend') 23 24 plt.subplot(413) 25 plt.plot(df['time'], res.seasonal, lw=.6, c='indianred') 26 plt.title('seasonal') 27 28 plt.subplot(414) 29 plt.plot(df['time'], res.resid, lw=.6, c='indianred') 30 plt.title('residual')

結果

![グラフ表示結果

ただ、seasonnalが異常な形をしているのでちゃんと出せているかは不安です…笑

ご回答のお礼

meg_さん。
ご回答ありがとうございます!
ご教授いただいたコードなど色々試すうちに、
plt.plot()を変えれば良いのではという考えにたどり着くことができました!
ありがとうございました!

※謎だったのが、

plt.xticks(np.arange(0,max(df.index),0.001))

で確かに0.001秒単位の数値を表示できる!と思ったのですが、
なぜか(何回か試しても)固まってしまいました。
(timeの値を文字として表示させる際にデータ容量が大きくなるからかもしれません…)

投稿2019/10/17 08:06

Taiga_s

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

X軸を掲載データのtime列の累計値(経過時間)としてグラフ化してみました。
※time列の値が昇順に並んでいるようですが正しいデータの並びでしょうか?

Python

1import pandas as pd 2import matplotlib.pyplot as plt 3import numpy as np 4 5df = pd.DataFrame({'time':[0.0002,0.0003,0.0003,0.013],'gFz':[1.101337,0.96714,0.9999,0.96133]}) 6df['time2'] = df['time'].cumsum() 7df = df.set_index(df['time2']) 8df = df.drop(['time','time2'],axis=1) 9 10plt.plot(df) 11plt.xticks(np.arange(0,max(df.index),0.001)) 12plt.show()

イメージ説明

投稿2019/10/16 11:07

meg_

総合スコア10580

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問