【发布时间】:2019-11-01 13:12:51
【问题描述】:
我正在尝试使用 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 社区版 IDE 上运行所有这些,并且我的操作系统是 Linux/Ubuntu 18.04。
我想知道如何解决这个问题以便能够显示图表。
【问题讨论】:
-
暂时忘记图表。您的问题是安装 tkinter。您是否尝试过任何可用的解决方案,例如stackoverflow.com/questions/4783810/install-tkinter-for-python ?
-
您实际上是在使用 tkinter 做任何事情,还是只是选择它作为 plt 后端?
-
@ImportanceOfBeingErnest:感谢您的提示。我确实会首先专注于安装 tkinter。我会看看你提供的链接,看看我能不能从中得到什么。
-
@G.Anderson:在使用 matplotlib 遇到此错误之前,我不知道 tkinter 是什么。现在我正在尝试安装它只是为了能够显示图表(所以是的,我想我只是选择它作为 plt 后端)。如果您知道任何其他方式(即不使用 tkinter),我会很高兴听到它。
-
@G.Anderson:我运行脚本来循环浏览不同的后端。事实证明,只有 TkAgg 可用!事实上,我在测试脚本之前刚刚安装了它。
标签: python matplotlib pycharm