我一直关注' python进行数据分析'。在pg。 345,你得到这个代码来绘制各种股票的回报。但是,绘图功能对我不起作用。我明白了 FigureCanvasAgg'对象没有属性'无效' ?
names = ['AAPL','MSFT', 'DELL', 'MS', 'BAC', 'C'] #goog and SF did not work
def get_px(stock, start, end):
return web.get_data_yahoo(stock, start, end)['Adj Close']
px = pd.DataFrame({n: get_px(n, '1/1/2009', '6/1/2012') for n in names})
#fillna method pad uses last valid observation to fill
px = px.asfreq('B').fillna(method='pad')
rets = px.pct_change()
df2 = ((1 + rets).cumprod() - 1)
df2.ix[0] = 1
df2.plot()
更新:完整追溯
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-122-df192c0432be> in <module>()
6 df2.ix[0] = 1
7
----> 8 df2.plot()
//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
1634 logy=logy, sort_columns=sort_columns,
1635 secondary_y=secondary_y, **kwds)
-> 1636 plot_obj.generate()
1637 plot_obj.draw()
1638 if subplots:
//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in generate(self)
854 self._compute_plot_data()
855 self._setup_subplots()
--> 856 self._make_plot()
857 self._post_plot_logic()
858 self._adorn_subplots()
//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _make_plot(self)
1238 if not self.x_compat and self.use_index and self._use_dynamic_x():
1239 data = self._maybe_convert_index(self.data)
-> 1240 self._make_ts_plot(data, **self.kwds)
1241 else:
1242 lines = []
//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _make_ts_plot(self, data, **kwargs)
1319 self._maybe_add_color(colors, kwds, style, i)
1320
-> 1321 _plot(data[col], i, ax, label, style, **kwds)
1322
1323 self._make_legend(lines, labels)
//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _plot(data, col_num, ax, label, style, **kwds)
1293 def _plot(data, col_num, ax, label, style, **kwds):
1294 newlines = tsplot(data, plotf, ax=ax, label=label,
-> 1295 style=style, **kwds)
1296 ax.grid(self.grid)
1297 lines.append(newlines[0])
//anaconda/lib/python2.7/site-packages/pandas/tseries/plotting.pyc in tsplot(series, plotf, **kwargs)
79
80 # set date formatter, locators and rescale limits
---> 81 format_dateaxis(ax, ax.freq)
82 left, right = _get_xlim(ax.get_lines())
83 ax.set_xlim(left, right)
//anaconda/lib/python2.7/site-packages/pandas/tseries/plotting.pyc in format_dateaxis(subplot, freq)
258 subplot.xaxis.set_major_formatter(majformatter)
259 subplot.xaxis.set_minor_formatter(minformatter)
--> 260 pylab.draw_if_interactive()
//anaconda/lib/python2.7/site-packages/IPython/utils/decorators.pyc in wrapper(*args, **kw)
41 def wrapper(*args,**kw):
42 wrapper.called = False
---> 43 out = func(*args,**kw)
44 wrapper.called = True
45 return out
//anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc in draw_if_interactive()
227 figManager = Gcf.get_active()
228 if figManager is not None:
--> 229 figManager.canvas.invalidate()
230
231
AttributeError: 'FigureCanvasAgg' object has no attribute 'invalidate'
答案 0 :(得分:21)
我发现此错误是由以下因素组合而成的:
%matplotlib inline
魔法因此,ipython笔记本中的新启动的内核会失败:
# fails
import matplotlib.pylab
%matplotlib inline
import pandas
ser = pandas.Series(range(10), pandas.date_range(end='2014-01-01', periods=10))
ser.plot()
解决这个问题的最佳方法是将魔法移到顶端:
# succeeds
%matplotlib inline # moved up
import matplotlib.pylab
import pandas
ser = pandas.Series(range(10), pandas.date_range(end='2014-01-01', periods=10))
ser.plot()
但是,如果将系列传递给matplotlib
绘图方法,不使用日期索引,或者只是不导入matplotlib.pylab
模块,问题也会消失。
答案 1 :(得分:2)
不是答案,但我无法弄清楚如何将代码块放入评论中:)
所以我得到了这个问题,因为我的Mac上发生了同样的事情
/Users/briford/myPVE/workbench/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc in draw_if_interactive()
227 figManager = Gcf.get_active()
228 if figManager is not None:
--> 229 figManager.canvas.invalidate()
230
AttributeError: 'FigureCanvasAgg' object has no attribute 'invalidate'
无论如何,我知道它并不令人满意,只是关闭了我的ipython笔记本服务并重新启动了它......耸耸肩......
答案 2 :(得分:1)
我似乎解决了这个问题(至少在我的情况下)。
我正在使用python 2.7在mac上运行IPython并且遇到了同样的错误。
后端似乎确实存在问题,因为当我查看“dock”时,已经打开了很多Python启动器的实例(不知道为什么这首先发生了)。
强制关闭它们会导致python内核重启,似乎已经解决了我的问题。
内嵌代码仍然到位且图表正确显示。
答案 3 :(得分:0)
其他答案对我不起作用。相反,我的问题是因为我用ipython notebook --pylab
启动笔记本。一旦我放弃--pylab
再次运作的东西。
因此,请确保只使用ipython notebook
启动ipython笔记本。
(当你使用--pylab
时实际上会发出警告,但直到现在我都错过了。)
答案 4 :(得分:0)
我通过在Jupyter Notebook的第一个代码单元中添加%matplotlib inline
并运行它来解决了该问题
答案 5 :(得分:0)
当我使用非交互式后端(例如matplotlib.use('svg')
)时出现此错误
对我来说,解决方法是更改后端(例如matplotlib.use('Qt5Agg')
)