joh*_*987 34 python matplotlib pycharm
I am trying to plot a simple graph using pyplot, e.g.:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
Run Code Online (Sandbox Code Playgroud)
but the figure does not appear and I get the following message:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
Run Code Online (Sandbox Code Playgroud)
I saw in several places that one had to change the configuration of matplotlib using the following:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
I did this, but then got an error message because it cannot find a module:
ModuleNotFoundError: No module named 'tkinter'
Run Code Online (Sandbox Code Playgroud)
Then, I tried to install "tkinter" using pip install tkinter
(inside the virtual environment), but it does not find it:
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
Run Code Online (Sandbox Code Playgroud)
I should also mention that I am running all this on Pycharm Community Edition IDE using a virtual environment, and that my operating system is Linux/Ubuntu 18.04.
I would like to know how I can solve this problem in order to be able to display the graph.
joh*_*987 46
我找到了解决问题的方法(借助于ImportanceOfBeingErnest的帮助)。
我要做的就是tkinter
使用以下命令通过Linux bash终端安装:
sudo apt-get install python3-tk
而不是pip
在Pycharm的虚拟环境中安装或直接安装。
dex*_*xed 35
如果您使用 Arch Linux(发行版如Manjaro
或Antegros
),只需键入:
sudo pacman -S tk
Run Code Online (Sandbox Code Playgroud)
一切都会完美运行!
小智 22
安装简单
pip3 install PyQt5==5.9.2
Run Code Online (Sandbox Code Playgroud)
这个对我有用。
小智 19
尝试import tkinter
因为 pycharm 已经为您安装了 tkinter,我查看了Install tkinter for Python
你也许可以试试:
import tkinter
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.plot([1,2,3],[5,7,4])
plt.show()
Run Code Online (Sandbox Code Playgroud)
作为 tkinter 安装方式
你的方法我试过了,在我的电脑上运行似乎没有错误,它成功地显示了这个图。也许是因为 pycharm 有 tkinter 作为系统包,所以你不需要安装它。但是如果你在里面找不到tkinter,你可以去Tkdocs看看安装tkinter的方法,正如上面提到的,tkinter是python的核心包。
小智 11
我在 PyCharm 中也有这个问题。这个问题是因为您的机器中没有 tkinter 模块。
按照下面给出的步骤进行安装(选择合适的操作系统)
对于 ubuntu 用户
sudo apt-get install python-tk
Run Code Online (Sandbox Code Playgroud)
或者
sudo apt-get install python3-tk
Run Code Online (Sandbox Code Playgroud)
Centos 用户
sudo yum install python-tkinter
Run Code Online (Sandbox Code Playgroud)
或者
sudo yum install python3-tkinter
Run Code Online (Sandbox Code Playgroud)
对于 Arch 用户
sudo pacman -S tk
Run Code Online (Sandbox Code Playgroud)
或者
sudo pamac install tk
Run Code Online (Sandbox Code Playgroud)
对于 Windows,使用 pip 安装 tk
安装 tkinter 后重启你的 Pycharm 并运行你的代码,它会工作
对于 Windows 10,如果使用pip install tk
不适合您,请尝试:
我的修复来源: https ://stackoverflow.com/a/59970646/2506354
小智 6
1: matplotlib.use( 'tkagg' )
或者
2: matplotlib$use( 'tkagg' )
例如:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import matplotlib
matplotlib.use( 'tkagg' )
style.use("ggplot")
from sklearn import svm
x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud)
就我而言,该错误消息表示我在无头控制台中工作。因此plt.show()
无法正常工作。起作用的是打电话plt.savefig
:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.savefig("mygraph.png")
Run Code Online (Sandbox Code Playgroud)
我在github仓库上找到了答案。
归档时间: |
|
查看次数: |
23639 次 |
最近记录: |