when i use matplotlib in jupyter notebook,it always raise “ matplotlib is currently using a non-GUI backend” error?

后端 未结 10 1377
耶瑟儿~
耶瑟儿~ 2020-11-28 07:40
import matplotlib.pyplot as pl
%matplot inline
def learning_curves(X_train, y_train, X_test, y_test):
\"\"\" Calculates the performance of several models with varyin         


        
相关标签:
10条回答
  • 2020-11-28 08:37

    Testing with https://matplotlib.org/examples/animation/dynamic_image.html I just add

    %matplotlib notebook
    

    which seems to work but is a little bumpy. I had to stop the kernal now and then :-(

    0 讨论(0)
  • 2020-11-28 08:37

    You can still save the figure by fig.savefig()

    If you want to view it on the web page, you can try

    from IPython.display import display
    display(fig)
    
    0 讨论(0)
  • 2020-11-28 08:41

    The error "matplotlib is currently using a non-GUI backend” also occurred when I was trying to display a plot using the command fig.show(). I found that in a Jupyter Notebook, the command fig, ax = plt.subplots() and a plot command need to be in the same cell in order for the plot to be rendered.

    For example, the following code will successfully show a bar plot in Out[5]:

    In [3]:

    import matplotlib.pyplot as plt
    %matplotlib inline
    

    In [4]:

    x = 'A B C D E F G H'.split()
    y = range(1, 9)
    

    In [5]:

    fig, ax = plt.subplots()
    ax.bar(x, y)
    

    Out[5]: (Container object of 8 artists)

    A successful bar plot output

    On the other hand, the following code will not show the plot,

    In [5]:

    fig, ax = plt.subplots()
    

    Out[5]:

    An empty plot with only a frame

    In [6]:

    ax.bar(x, y)
    

    Out[6]: (Container object of 8 artists)

    In Out[6] there is only a statement of "Container object of 8 artists" but no bar plot is shown.

    0 讨论(0)
  • 2020-11-28 08:46

    I had the same error. Then I used

    import matplotlib matplotlib.use('WebAgg')

    it works fine.(You have to install tornado to view in web, (pip install tornado))

    Python version: 3.7 matplotlib version: 3.1.1

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