我正在try 使用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'

然后,我try 使用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/Ubuntu18.04.

I would like to know how I can solve this problem in order to be able to display the graph.

推荐答案

Solution 1: is to install the GUI backend tk

我找到了解决问题的办法(多亏了ImportanceOfBeingErnest的帮助).

我所要做的就是使用以下命令通过Linux bash终端安装tkinter:

sudo apt-get install python3-tk

而不是用pip或直接在Pycharm的虚拟环境中安装.

Solution 2: install any of the matplotlib supported GUI backends

  • 解决方案1运行良好,因为您有一个GUI后端...在这种情况下,TkAgg
  • 不过,您也可以通过安装任何matplolib GUI后端(如Qt5AggGTKAggQt4Agg等)来解决这个问题

注:

  • 通常,在pip安装matplotlib时,当您试图在GUI窗口中显示绘图,并且没有用于GUI显示的python模块时,会出现此错误.
  • matplotlib的作者使pypi软件开发人员不依赖任何GUI后端,因为有些人need-matplotlib没有任何GUI后端.

Python相关问答推荐

如何使用bs 4从元素中提取文本

从今天起的future 12个月内使用Python迭代

优化在numpy数组中非零值周围创建缓冲区的函数的性能

理解Python的二分库:澄清bisect_left的使用

大Pandas 胚胎中产生组合

scikit-learn导入无法导入名称METRIC_MAPPING64'

更改键盘按钮进入'

把一个pandas文件夹从juyter笔记本放到堆栈溢出问题中的最快方法?

启用/禁用shiny 的自动重新加载

Python列表不会在条件while循环中正确随机化'

Plotly Dash Creating Interactive Graph下拉列表

Maya Python脚本将纹理应用于所有对象,而不是选定对象

为什么常规操作不以其就地对应操作为基础?

如何找出Pandas 图中的连续空值(NaN)?

处理Gekko的非最优解

我对这个简单的异步者的例子有什么错误的理解吗?

如何从比较函数生成ngroup?

使用SQLAlchemy从多线程Python应用程序在postgr中插入多行的最佳方法是什么?'

极柱内丢失类型信息""

将字节序列解码为Unicode字符串