FigureCanvasAgg' object has no attribute 'invalidate' ? python plotting

后端 未结 6 849
无人及你
无人及你 2021-02-05 12:19

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

相关标签:
6条回答
  • 2021-02-05 12:47

    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.

    0 讨论(0)
  • 2021-02-05 12:50

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

    0 讨论(0)
  • 2021-02-05 12:52

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

    0 讨论(0)
  • 2021-02-05 12:52

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

    0 讨论(0)
  • 2021-02-05 13:03

    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.

    0 讨论(0)
  • 2021-02-05 13:09

    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...

    0 讨论(0)
提交回复
热议问题