【问题标题】:Error with matplotlib when running examples of scikit learn运行 scikit learn 示例时 matplotlib 出错
【发布时间】:2015-08-25 22:45:39
【问题描述】:

我最近在学习 python,我遇到了一个名为 scikit learn 的包,我们可以在其中使用 python 库和自定义代码来生成各种绘图。我已经安装了所有依赖项,然后我下载并安装了 scikit learn 但是当我尝试运行示例代码时,我在生成绘图时遇到错误。

代码

from sklearn import datasets
from sklearn.cross_validation import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()
boston = datasets.load_boston()
y = boston.target

# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validated:
predicted = cross_val_predict(lr, boston.data, y, cv=10)

fig,ax = plt.subplots() ## error comes from calling the function plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
fig.show()

错误

fig,ax = plt.subplots()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 866, in subplots
    fig = figure(**fig_kw)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

这与设置环境变量有关吗?我正在使用python 2.7.3,这个包应该可以使用它还是我必须放置路径或其他东西?抱歉,我对 python 很陌生,只是想很快弄清楚我是否可以使用这个包来快速生成绘图。感谢您为摆脱此错误提供任何帮助。

【问题讨论】:

  • 这似乎不是scikit-learn 问题。从 matplotlib 创建图时存在问题。你确定安装正确吗?
  • 是的,这是 matplotlib 的问题,而不是 scikit-learn 的问题。我已经通过以下命令 {pip install --user --install-option="--prefix=" -U scikit-learn} 在 linux 中安装了 scikit-learn,安装没有任何问题。然后我直接使用示例中的脚本在 python2.7.3 上运行它们。我在脚本中导入 matplotlib 应该没有问题吧?

标签: python python-2.7 scikit-learn


【解决方案1】:

我怀疑您正试图在远程机器上运行您的代码(ssh 可能)或者只是 matplotlib 没有安装好。

如果你是第一种情况,我建议使用savefig 和命令matplotlib.use('Agg'),告诉matplotlib 不要使用默认显示。这将生成一个.png 文件,您可以使用scp 将其导入您的家中:

import matplotlib
matplotlib.use('Agg')
from pylab import savefig
from sklearn import datasets
from sklearn.cross_validation import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt

lr = linear_model.LinearRegression()
boston = datasets.load_boston()
y = boston.target

# cross_val_predict returns an array of the same size as `y` where each entry
# is a prediction obtained by cross validated:
predicted = cross_val_predict(lr, boston.data, y, cv=10)

fig,ax = plt.subplots() ## error comes from calling the function plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')

# don't use fig.show()

savefig('FIGURE_NAME.png') # savefig from matplotlib

【讨论】:

  • 是的,我正在 ssh 集群中使用。但是,一旦您下载了 scikit-learn,matplotlib 库是否会直接安装?应该是这样,您只需要在运行脚本时导入库
  • yes matplotlib 已安装,但要显示绘图,python 找不到显示它的位置 :-)
  • 那我应该怎么改呢?我需要把路径放在 plt.subplots() 对吗?
  • 但现在遇到了一些其他问题。 ax.scatter(y, predicted) &lt;matplotlib.collections.PathCollection object at 0x35bc0d0&gt; &gt;&gt;&gt; ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4) [&lt;matplotlib.lines.Line2D object at 0x35bc050&gt;] &gt;&gt;&gt; ax.set_xlabel('Measured') &lt;matplotlib.text.Text object at 0x3432210&gt; &gt;&gt;&gt; ax.set_ylabel('Predicted') &lt;matplotlib.text.Text object at 0x3435c10&gt; &gt;&gt;&gt; savefig('FIGURE_NAME.png') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'savefig' is not defined 。为什么会出现这个错误?
  • 抱歉,您需要添加from paylab import savefig
猜你喜欢
  • 2012-06-29
  • 1970-01-01
  • 2022-06-10
  • 2018-01-09
  • 1970-01-01
  • 2017-10-15
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多