我试图用pyplot绘制一个简单的图形,例如:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()

但是这个图没有出现,我得到了以下消息:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

我在几个地方看到必须使用以下命令更改matplotlib的配置:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

我这样做了,但得到了一个错误消息,因为它找不到一个模块:

ModuleNotFoundError: No module named 'tkinter'

然后,我尝试使用pip install tkinter(在虚拟环境中)安装“tkinter”,但它没有找到它:

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

我还应该提到,我是在使用虚拟环境的Pycharm Community Edition IDE上运行所有这些,并且我的操作系统是Linux/Ubuntu 18.04。

我想知道我如何解决这个问题,以便能够显示图形。


当前回答

这个问题的解决方法是: 确保把这条线放好

%matplotlib inline

在代码头部

像这样

# In an IPython notebook¶
# This magic just sets up matplotlib's interactive mode
%matplotlib inline
# So you have to explicitely import the module into the namespace
import matplotlib.pyplot as pl
import numpy as np
# Create the figure object
fig = pl.figure(figsize=(12, 8))
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
pl.plot(x, y)

%matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. This has important implications for interactivity: for inline plotting, commands in cells below the cell that outputs a plot will not affect the plot. For example, changing the color map is not possible from cells below the cell that creates a plot. However, for other backends, such as qt4, that open a separate window, cells below those that create the plot will change the plot - it is a live object in memory. If you are not using matplotlib in interactive mode at all, figures will only appear if you invoke

有关%matplotlib内联的更多信息,请参阅此链接

其他回答

运行 % matplotlib内联 曾经为我解决了这个问题。 我在这里找到了答案:当我在jupyter笔记本电脑中使用matplotlib时,它总是引发“matplotlib目前正在使用非gui后端”错误? 由用户Mulugeta Weldezgina

在导入时内联添加%matplotlib有助于平滑绘图 笔记本

%matplotlib inline
import matplotlib.pyplot as plt

%matplotlib inline将matplotlib后端设置为“内联” 后端: 有了这个后端,绘图命令的输出将内联显示 就像Jupyter笔记本一样,在代码的正下方 产生它的细胞。生成的图形也将存储在 笔记本文档。

我的问题开始后,我使用pandas_profile(或类似的东西),运行%matplotlib内联一次固定的背景从无头等。

issue = "用户警告:Matplotlib目前正在使用agg,这是非gui后端,所以不能显示图形。"

这对我很有效

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')

小心你的代码中的导入顺序,我花了一整天的时间通过这个答案,最终通过在其他任何东西之前导入bt,然后使用.use('TkAgg')语句解决了问题(由于某种原因,导入bt将matplotlib后端更改为'Agg')

简单的安装

pip3 install PyQt5==5.9.2

这对我很管用。

这个问题的解决方法是: 确保把这条线放好

%matplotlib inline

在代码头部

像这样

# In an IPython notebook¶
# This magic just sets up matplotlib's interactive mode
%matplotlib inline
# So you have to explicitely import the module into the namespace
import matplotlib.pyplot as pl
import numpy as np
# Create the figure object
fig = pl.figure(figsize=(12, 8))
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
pl.plot(x, y)

%matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. This has important implications for interactivity: for inline plotting, commands in cells below the cell that outputs a plot will not affect the plot. For example, changing the color map is not possible from cells below the cell that creates a plot. However, for other backends, such as qt4, that open a separate window, cells below those that create the plot will change the plot - it is a live object in memory. If you are not using matplotlib in interactive mode at all, figures will only appear if you invoke

有关%matplotlib内联的更多信息,请参阅此链接