FigureCanvasAgg' object has no attribute 'invalidate' ? python plotting
Asked Answered
P

6

15

I've been following 'python for data analysis'. On pg. 345, you get to this code to plot returns across a variety of stocks. However, the plotting function does not work for me. I get FigureCanvasAgg' object has no attribute 'invalidate' ?

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()

UPDATE: full traceback

---------------------------------------------------------------------------
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'
Pleinair answered 8/5, 2014 at 18:43 Comment(3)
Please paste in the full tracebackSeidule
Something is messed up in your install or pandas is doing something funny in constructing your figures. The macosx backend should not have an Agg canvas object. What does matplotlib.get_backend() return?Seidule
@tcaswell I reported this as github.com/matplotlib/matplotlib/issues/4156Stereoscope
S
23

I found this error to be due to a combination of:

  • using pandas plotting with a series or dataframe member method
  • plotting with a date index
  • using %matplotlib inline magic in ipython
  • importing the pylab module before the matplotlib magic

So the following will fail on a newly started kernel in an ipython notebook:

# fails 
import matplotlib.pylab
%matplotlib inline

import pandas
ser = pandas.Series(range(10), pandas.date_range(end='2014-01-01', periods=10))
ser.plot()

The best way to solve this is to move the magic up to the top:

# 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()

However the problem also goes away if you pass the series to a matplotlib plotting method, don't use a date index, or simply don't import the matplotlib.pylab module.

Sulfanilamide answered 31/7, 2014 at 13:58 Comment(6)
Actually I'm getting the issue with matplotlib magic on top of the code. I see I cannot paste code in a comment, still the point is valid, having %matplotlib on top isn't necessary for the issue.Windfall
Nevermind, running all cells again doesn't show the issue anymore on my setup. I had actually preferred to have a reproducible issue so it could have been fixed.Windfall
Has this been reported as a bug against ipython or matplotlib or both? (if the answer is 'neither', we should fix that ;-)) Has anyone experienced this when not using Anaconda or another Conda-based setup?Chubby
I experience this outside of Anaconda.Erst
For the record, I got the same error, and the same fix worked, with a slightly different setup (though maybe the two are the same behind the scenes). %pylab inline and import seaborn will do it as well.Gardas
that's what saves my day! I don't remember such behavior in python 2 but maybe I just didn't notice.Aisne
P
2

Not an answer but I can't figure out how to put code block in comments :)

So I got to this question because the exact same thing was happening on my 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'

Anyway I know it's not satisfying but just doing a shutdown of my ipython notebook service and doing a restart cleared it up... shrug...

Profiteer answered 28/5, 2014 at 19:21 Comment(0)
D
1

I seemed to resolve the issue (well at least in my case).

I'm running IPython on a mac using python 2.7 and was getting the same error.

It did seem to be an issue with the backend as when I looked at the "dock", quite a few instances of the Python Launcher had been opened (not sure why this happened in the first place though).

Forcing those to close caused the python kernel to restart and has seemed to have fixed my issue.

The inline code is still in place and plots are showing correctly.

Dowson answered 10/3, 2016 at 15:47 Comment(1)
Yes you've got it! On a mac we should close those extras Python Interpreter instances.Threequarter
S
0

The other answers didn't work for me. Instead my problem was because I was starting the notebook with ipython notebook --pylab. Once I dropped the --pylab things worked again.

So make sure you start ipython notebook with only ipython notebook.

(There's actually a warning emitted when you use --pylab but I missed it until now.)

Stereoscope answered 25/2, 2015 at 22:54 Comment(0)
R
0

I resolved the issue by adding %matplotlib inline on the first code cell of jupyter notebook and running it

Resuscitator answered 18/3, 2020 at 16:58 Comment(0)
E
0

I have this error when I use a non-interactive backend (e.g. matplotlib.use('svg'))

Fix for me was to change the backend (e.g. matplotlib.use('Qt5Agg'))

Evacuation answered 26/3, 2020 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.