Python 用户警告:Matplotlib当前正在使用agg,因此无法显示该图形

Python 用户警告:Matplotlib当前正在使用agg,因此无法显示该图形,python,python-3.x,matplotlib,tkinter,Python,Python 3.x,Matplotlib,Tkinter,我正在尝试从以下位置运行一个基本的matplotlib示例: 但是,当我运行代码时,我的Python解释器会抱怨并输出以下消息: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show() 我已通过pip3安装matplotlib安装了matplotlib。 我目前的python3版本是3.9.1,我的操作系统是Ubuntu 2

我正在尝试从以下位置运行一个基本的
matplotlib
示例:

但是,当我运行代码时,我的Python解释器会抱怨并输出以下消息:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
我已通过
pip3安装matplotlib
安装了
matplotlib
。 我目前的
python3
版本是3.9.1,我的操作系统是Ubuntu 20.04

如前所述,我已经尝试安装tkinter,但没有成功。
我该怎么办?为什么会发生这种情况?

如果有适合您的方法,请尝试以下方法:

  • 如果您使用的是Jupyter笔记本

     %matplotlib inline 
    
  • 确保您有tkinter,在安装tkinter后重新编译python解释器

  • 尝试:

     import matplotlib
     import matplotlib.pyplot as plt
     plt.style.use('ggplot')
     matplotlib.use( 'tkagg' )
     x = [1, 5, 1.5, 4]
     y = [9, 1.8, 8, 11]
     plt.scatter(x,y)
     plt.show()
    

  • 如何重新编译python解释器?我没有使用jupyter笔记本,即使在添加了这些建议行之后,我仍然会得到
    import\u tkinter\35;如果失败,您的Python可能没有为Tk ModuleNotFoundError配置:没有名为“\u tkinter”的模块。
    为什么在tkinter之前使用下划线?我不确定这是它给出的错误。我运行的
    matplotlib
    示例与网站中描述的完全相同