我试图用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.use('Agg')

搜索它并注释掉它。

如果您不知道它是什么,那么您可能不会使用这部分代码。

使用另一个后端GUI的解决方案可能更干净,所以选择你的战斗机。

其他回答

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

%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内联的更多信息,请参阅此链接

在使用Pycharm专业版2021.3时,这些答案对我都不起作用

常规的matplotlib图表在科学观点上确实有效,但它不允许我向图表中添加图像。

对我来说有用的是在我尝试绘制任何东西之前添加这条线:

plt switch_backend (TkAgg’)。

@xicocaio的评论应该突出显示。

tkinter is python version-specific in the sense that sudo apt-get install python3-tk will install tkinter exclusively for your default version of python. Suppose you have different python versions within various virtual environments, you will have to install tkinter for the desired python version used in that virtual environment. For example, sudo apt-get install python3.7-tk. Not doing this will still lead to No module named ' tkinter' errors, even after installing it for the global python version.

尝试导入tkinter,因为pycharm已经为您安装了tkinter,我查看了为Python安装tkinter

你可以试试:

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

作为一种tkinter安装方式

我已经试过你的方法了,在我的电脑上运行似乎没有错误,它成功地显示了这个图形。可能是因为pycharm有tkinter作为系统包,所以你不需要安装它。但是如果你在里面找不到tkinter,你可以去Tkdocs看看tkinter的安装方法,正如它提到的,tkinter是python的核心包。

Ubuntu 20.04命令行设置。我安装了以下程序,以使Matplotlib停止抛出错误UserWarning: Matplotlib目前正在使用agg,这是非gui后端,因此无法显示图形。

我通过以下步骤安装python-tk:

apt-get update

apt-get install python3.8-tk