移動平均、出来高、MACD、RSIの4つのグラフを出力したいのですが、エラー文と共に最後のRSIのみグラフが出てきません。エラー文もよくわからないので、どうしたらよいでしょうか。
import pandas as pd import numpy as np import talib as ta from pandas_datareader import data import matplotlib.pyplot as plt %matplotlib inline start = '2019-11-01' end = '2020-11-01' df = data.DataReader('^N225', 'yahoo', start, end) date = df.index close = df['Adj Close'] df['macd'], df['macdsignal'], df['macdhist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) df['RSI'] = ta.RSI(close, timeperiod=span02) span01 = 5 span02 = 25 span03 = 50 df['mov01'] = close.rolling(window=span01).mean() df['mov02'] = close.rolling(window=span02).mean() df['mov03'] = close.rolling(window=span03).mean() plt.figure(figsize=(30,10)) plt.subplot(4,1,1) #移動平均 plt.plot(date,close,label='Close') plt.plot(date, df['mov01'],label='mov01') plt.plot(date, df['mov02'],label='mov02') plt.plot(date, df['mov03'],label='mov03') plt.legend() plt.subplot(4,1,2) #出来高 plt.bar(date, df['Volume'], label='Volume') plt.legend() plt.subplot(4,1,3) #MACD plt.fill_between(date, df['macdhist'], alpha=0.5, label='MACD_hist') plt.hlines(0, start, end, 'gray', linestyle='dashed') plt.legend() plt.subplot(4,1,4) #RSI plt.plot(date, df['RSI'], label='RSI') plt.ylim(0,100) plt.hlines([30,50,70], start, end, 'gray', linestyles='dashed') plt.legend()
エラー文は以下です。
AttributeError 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\dates.py in convert(value, unit, axis) 2007 """ -> 2008 return date2num(value) 2009 ~\Anaconda3\lib\site-packages\matplotlib\dates.py in date2num(d) 425 return d --> 426 return _to_ordinalf_np_vectorized(d) 427 ~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in __call__(self, *args, **kwargs) 2107 -> 2108 return self._vectorize_call(func=func, args=vargs) 2109 ~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in _vectorize_call(self, func, args) 2185 else: -> 2186 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args) 2187 ~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in _get_ufunc_and_otypes(self, func, args) 2145 inputs = [arg.flat[0] for arg in args] -> 2146 outputs = func(*inputs) 2147 ~\Anaconda3\lib\site-packages\matplotlib\dates.py in _to_ordinalf(dt) 225 --> 226 base = float(dt.toordinal()) 227 AttributeError: 'numpy.str_' object has no attribute 'toordinal' The above exception was the direct cause of the following exception: ConversionError Traceback (most recent call last) <ipython-input-11-21219a718bff> in <module> 38 plt.subplot(4,1,3) #MACD 39 plt.fill_between(date, df['macdhist'], alpha=0.5, label='MACD_hist') ---> 40 plt.hlines(0, start, end, 'gray', linestyle='dashed') 41 plt.legend() 42 ~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in hlines(y, xmin, xmax, colors, linestyles, label, data, **kwargs) 2664 y, xmin, xmax, colors=colors, linestyles=linestyles, 2665 label=label, **({"data": data} if data is not None else {}), -> 2666 **kwargs) 2667 2668 ~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs) 1599 def inner(ax, *args, data=None, **kwargs): 1600 if data is None: -> 1601 return func(ax, *map(sanitize_sequence, args), **kwargs) 1602 1603 bound = new_sig.bind(ax, *args, **kwargs) ~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in hlines(self, y, xmin, xmax, colors, linestyles, label, **kwargs) 1098 self._process_unit_info([xmin, xmax], y, kwargs=kwargs) 1099 y = self.convert_yunits(y) -> 1100 xmin = self.convert_xunits(xmin) 1101 xmax = self.convert_xunits(xmax) 1102 ~\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: '2019-11-01'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/07 06:34