python - Error with matplotlib when running exmaples of scikit learn -
i learning python, , came across package called scikit learn can use python libraries , custom made codes generate various plots. have installed dependancies , have downloaded , installed scikit learn when trying run example codes getting error in generating plot.
code
from sklearn import datasets sklearn.cross_validation import cross_val_predict sklearn import linear_model import matplotlib.pyplot plt lr = linear_model.linearregression() boston = datasets.load_boston() y = boston.target # cross_val_predict returns array of same size `y` each entry # prediction obtained cross validated: predicted = cross_val_predict(lr, boston.data, y, cv=10) fig,ax = plt.subplots() ## error comes calling 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()
error
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 , no $display environment variable
is setting environmental variable? using python 2.7.3 , package should work or doing have put path or something? sorry new python trying figure out if can use package generating plots or not. rid of error appreciated.
i suspect trying run code on distant machine (ssh
maybe) or matplotlib
not installed.
if in first case, suggest use savefig
command matplotlib.use('agg')
tells matplotlib
not use default display. produce .png
file import home using scp
:
import matplotlib matplotlib.use('agg') pylab import savefig sklearn import datasets sklearn.cross_validation import cross_val_predict sklearn import linear_model import matplotlib.pyplot plt lr = linear_model.linearregression() boston = datasets.load_boston() y = boston.target # cross_val_predict returns array of same size `y` each entry # prediction obtained cross validated: predicted = cross_val_predict(lr, boston.data, y, cv=10) fig,ax = plt.subplots() ## error comes calling 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 matplotlib
Comments
Post a Comment