如果您在Jupyter Ipython笔记本上运行,则只需在代码顶部添加%matplotlib内联.您可以阅读有关它的更多信息here和here.
否则,我复制了你的代码并删除了matplotlib.use(“Agg”),它适用于Ubuntu 18.04,matplotlib 2.2.2版.你能指定你使用的是哪个版本吗?
这里还有代码,
import matplotlib
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
#Generate Sample data
x = np.sort(5 * np.random.rand(40, 1), axis = 0)
y = np.sin(x).ravel()
#Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(8))
#create classifier regression model
svr_rbf = SVR(kernel="rbf", C=1000, gamma=0.1)
svr_lin = SVR(kernel="linear", C=1000, gamma=0.1)
svr_poly = SVR(kernel="poly", C=1000, gamma=0.1)
#Fit regression model
y_rbf = svr_rbf.fit(x,y).predict(x)
y_lin = svr_lin.fit(x,y).predict(x)
y_poly = svr_poly.fit(x,y).predict(x)
#Plotting of results
lw = 2
plt.scatter(x, y, color="darkorange", label="data")
plt.plot(x, y_rbf, color="navy", lw=lw, label="RBF Model")
plt.plot(x, y_lin, color="c", lw=lw, label="Linear Model")
plt.plot(x, y_poly, color="cornflowerblue", lw=lw, label="Polynomial Model")
plt.xlabel("data")
plt.ylabel("target")
plt.title("Support Vector Regression")
plt.legend()
plt.show()