【问题标题】:UserWarning: Matplotlib is currently using agg, so cannot show the figureUserWarning: Matplotlib 当前使用的是 agg,所以无法显示图
【发布时间】:2021-03-12 20:13:14
【问题描述】:

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

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

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

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

here 所述,我已经尝试安装 tkinter,但没有成功。 我该怎么办?为什么会这样?

【问题讨论】:

    标签: python python-3.x matplotlib tkinter


    【解决方案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()
      

    【讨论】:

    • 如何重新编译python解释器?即使添加了这些建议的行,我也没有使用 jupyter notebook 我仍然收到import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'
    • 为什么在 tkinter 之前使用下划线?
    • 我不是这是它给出的错误。我正在运行matplotlib 示例,与网站中描述的完全相同
    猜你喜欢
    • 2020-01-08
    • 2020-06-28
    • 2019-08-21
    • 2019-07-02
    • 2022-11-10
    • 2019-11-01
    • 2022-12-01
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多