#実現したい内容
以下のcsvを利用してX軸をdtimeのうち、日付を取り除いた00:00:00~23:59:59,y軸をwdata
で散布図のプロットをしたいです。
python
1import pandas as pd 2import matplotlib.pyplot as plt 3from matplotlib import dates as mdates 4from datetime import datetime as dt 5import numpy as np 6import datetime 7 8df = pd.read_csv("39534656_01.csv") 9 10xlist=df['dtime'].str[11:] 11 12ylist=df['wdata'] 13 14#必要かもしれない?↓ 15#xlist=str(xlist) 16 17#時間(文字列)をtime型に変換 18xlist=datetime.datetime.strptime(xlist, '%H:%M:%S').time() 19 20# データをプロット 21ax = plt.subplot() 22ax.plot_date(xlist,ylist) 23 24# X軸の設定 (目盛りを1時間毎,範囲は 0:00~23:59とする) 型がdt変更いるかもしれません 25ax.xaxis.set_major_locator(mdates.HourLocator()) 26ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) 27ax.set_xlim([dt.strptime('00:00','%H:%M'), dt.strptime('23:59','%H:%M')]) 28 29plt.title("39534656") 30plt.xlabel("dtime") 31plt.ylabel("wdata") 32plt.xticks(rotation=90) 33plt.show() 34 35#print(xlist) 36
#エラーメッセージ(いろいろ問題はあるが、、、)
TypeError Traceback (most recent call last) ~\anaconda3\lib\site-packages\matplotlib\axis.py in convert_units(self, x) 1549 try: -> 1550 ret = self.converter.convert(x, self.units, self) 1551 except Exception as e: ~\anaconda3\lib\site-packages\matplotlib\category.py in convert(value, unit, axis) 64 # force an update so it also does type checking ---> 65 unit.update(values) 66 ~\anaconda3\lib\site-packages\matplotlib\category.py in update(self, data) 218 if not isinstance(val, (str, bytes)): --> 219 raise TypeError("{val!r} is not a string".format(val=val)) 220 if convertible: TypeError: datetime.datetime(1900, 1, 1, 0, 0) is not a string The above exception was the direct cause of the following exception: ConversionError Traceback (most recent call last) <ipython-input-32-9cd6ffb7b246> in <module> 25 ax.xaxis.set_major_locator(mdates.HourLocator()) 26 ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M")) ---> 27 ax.set_xlim([dt.strptime('00:00','%H:%M'), dt.strptime('23:59','%H:%M')]) 28 29 plt.title("39534656") ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in set_xlim(self, left, right, emit, auto, xmin, xmax) 3239 3240 self._process_unit_info(xdata=(left, right)) -> 3241 left = self._validate_converted_limits(left, self.convert_xunits) 3242 right = self._validate_converted_limits(right, self.convert_xunits) 3243 ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _validate_converted_limits(self, limit, convert) 3153 """ 3154 if limit is not None: -> 3155 converted_limit = convert(limit) 3156 if (isinstance(converted_limit, Real) 3157 and not np.isfinite(converted_limit)): ~\anaconda3\lib\site-packages\matplotlib\artist.py in convert_xunits(self, x) 178 if ax is None or ax.xaxis is None: 179 return x --> 180 return ax.xaxis.convert_units(x) 181 182 def convert_yunits(self, y): ~\anaconda3\lib\site-packages\matplotlib\axis.py in convert_units(self, x) 1551 except Exception as e: 1552 raise munits.ConversionError('Failed to convert value(s) to axis ' -> 1553 f'units: {x!r}') from e 1554 return ret 1555 ConversionError: Failed to convert value(s) to axis units: datetime.datetime(1900, 1, 1, 0, 0)
#問題点
・型をdatetimeかtime型どうすればいいのかわからない
・どこをいじってもなにかしらエラーが出る
・業務の進行上もはや答えをしりたい…(笑)
情けない質問ですが、どうかお助けください????
あなたの回答
tips
プレビュー