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

时间:2021-03-12 20:13:14

标签: python python-3.x matplotlib tkinter

我正在尝试运行 official website 中的基本 matplotlib 示例:

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

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

我已通过 matplotlib 安装了 pip3 install matplotlib。 我当前的 python3 版本是 3.9.1,我的操作系统是 Ubuntu 20.04。

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

1 个答案:

答案 0 :(得分:1)

请试试这些,如果有的话:

  1. 如果您使用的是 Jupyter Notebook

     %matplotlib inline 
    
  2. 确保你有 tkinter,安装 tkinter 后重新编译 python 解释器

  3. 试试:

     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()
    
相关问题