When I use matplotlib in jupyter notebook,it always raise " matplotlib is currently using a non-GUI backend" error?

You don't need the line of "fig.show()". Just remove it. Then it will be no warning message.


adding %matplotlib inline while importing helps for smooth plots in notebook

%matplotlib inline
import matplotlib.pyplot as plt

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.


You can change the backend used by matplotlib by including:

import matplotlib
matplotlib.use('TkAgg')

before your line 1 import matplotlib.pyplot as pl, as it must be set first. See this answer for more information.

(There are other backend options, but changing backend to TkAgg worked for me when I had a similar problem)