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

后端 未结 6 854
无人及你
无人及你 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.

提交回复
热议问题