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 …
当尝试使用 Tkinter 标准库包或其相关功能(海龟图形使用turtle和内置 IDLE IDE)或使用将此作为依赖项的第三方库(例如显示使用 Matplotlib 的图形窗口)。
似乎即使不存在因隐藏标准库模块名称而引起的问题(对于尝试遵循教程并使用海龟图形的初学者来说,这是一个常见问题 -示例 1;示例 2;示例 3;示例 4),通常会发生标准库 Tkinter 无法工作的情况。这是一个大问题,因为许多初学者再次尝试遵循使用海龟图形的教程,并盲目地假设turtle标准库将会存在。
可能会报错:
作为ModuleNotFoundError: No module named 'tkinter'; 或ImportError具有相同消息的 ;或使用不同的大小写(我知道名称从Tkinter2.x 更改为tkinter3.x;这是一个不同的问题)。
类似地,但引用内部_tkinter模块,并显示带有注释的代码“如果失败,您的 Python 可能无法配置 Tk”;或者使用自定义错误消息“请安装 python-tk 包”或类似内容。
当尝试turtle专门使用时,出现“没有名为turtle的模块” ,或者上述错误之一。
当尝试使用 Matplotlib 显示绘图时;通常,这种情况会在尝试更改后端后发生,默认设置是为了避免尝试使用 Tkinter。
当 Tkinter 被记录为标准库的一部分时,为什么会出现这样的问题?如何添加或修复缺少的标准库功能?对于特定的 …
[(几个月前基本上与此相同:导入matplotlib在heroku上没有名为_tkinter的模块失败 .但是,提供的唯一解决方案似乎不起作用.(不幸的是,我不能评论那里给出的答案,因为我没有足够的StackOverflow信誉.))]
我一直在我的应用程序中使用matplotlib进行策划.一切都在当地很好.但是,当我将我的应用程序推送到Heroku时,我得到错误:
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
Run Code Online (Sandbox Code Playgroud)
我试图绕过Tkinter:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt, mpld3
Run Code Online (Sandbox Code Playgroud)
但是,这仍然会引发同样的错误.
有没有人为此找到了解决方案或者有matplotlib的Heroku应用程序正在运行?我正在运行Python 2.7.13(这也是Heroku在推送应用程序时安装的版本).