我正在关注本教程:https://www.youtube.com/watch?v=wfTABU8VeoY&list=PLQVvvaa0QuDfHt4XU7vTm22xDegR0v0fQ&index=7用于使用pandas进行数据分析,但是当我想运行以下代码时
import datetime
import pandas as pd
from pandas import DataFrame
import pandas.io.data
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sp500 = pd.io.data.get_data_yahoo('%5EGSPC', start = datetime.datetime(2015, 10, 15),
end = datetime.datetime(2016, 10, 15))
sp500.to_csv('sp500.csv')
df = pd.read_csv('sp500.csv', index_col = 'Date', parse_dates=True)
df['H-L'] = df['High'] - df.Low
df['100MA'] = pd.rolling_mean(df['Close'], 100)
df['Difference'] = df['Close'].diff()
threedee = plt.figure().gca(projection='3d')
threedee.scatter(df.index, df['H-L'], df['Close'])
threedee.set_xlabel('Index')
threedee.set_zlabel('Close')
threedee.set_ylabel('H-L')
plt.show()
它在Jupyter笔记本和PyCharm中产生如下错误:
OverflowError Traceback (most recent call last)
C:\Program Files\Anaconda2\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
C:\Program Files\Anaconda2\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
225
226 if 'png' in formats:
--> 227 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
228 if 'retina' in formats or 'png2x' in formats:
229 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
C:\Program Files\Anaconda2\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
117
118 bytes_io = BytesIO()
--> 119 fig.canvas.print_figure(bytes_io, **kw)
120 data = bytes_io.getvalue()
121 if fmt == 'svg':
还有许多其他路径,包括matplotlib.py和on。 怎么了?加载的数据不是太多,是吗?
答案 0 :(得分:0)
您是否尝试过更换此行
threedee.scatter(df.index, df['H-L'], df['Close'])
以下?
threedee.scatter(range(len(df.index)), df['H-L'], df['Close'])
您正在将时间戳绘制为值。 matplotlib
可能无法理解时间戳带来的数值。
编辑:遗憾的是,此解决方法将此x解决方案转换为数字范围。但我们总是可以手动设置滴答:
threedee.scatter(df.index, df['H-L'], df['Close'])
renderer = fig.canvas.get_renderer()
threedee.draw(renderer)
old_xticks = [t.get_text() for t in threedee.xaxis.get_ticklabels()]
new_xticks = [df.index[int(t)].strftime("%Y-%m-%d")
if t is not '' else '' for t in old_xticks]
threedee.xaxis.set_ticklabels(new_xticks)
threedee.set_xlabel('Index')
threedee.set_zlabel('Close')
threedee.set_ylabel('H-L')
plt.show()