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