简体   繁体   English

在 plt.plot() 方法中传递的 arguments 是什么意思?

[英]What do the arguments passed inside the plt.plot() method mean?

I was trying to plot a linear regression model in python using scikit learn and matplotlib.我试图在 python 中使用 scikit learn 和 ZF02113237A5A5FFF40ZEB4643 对 plot 进行线性回归 model。 However, the code got confusing when I was plotting the data using plt.scatter() and plt.plot()但是,当我使用 plt.scatter() 和 plt.plot() 绘制数据时,代码变得混乱

Here's my code which models the data using sklearn:-这是我的代码,它使用 sklearn 对数据进行建模:-

from sklearn import linear_model
regr = linear_model.LinearRegression()
train_x = np.asanyarray(train[['ENGINESIZE']])
train_y = np.asanyarray(train[['CO2EMISSIONS']])
regr.fit (train_x, train_y)
# The coefficients
print ('Coefficients: ', regr.coef_)
print ('Intercept: ',regr.intercept_)

Here's my code which plots the linear regression model on a graph:-这是我在图表上绘制线性回归 model 的代码:-

plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-y')
plt.xlabel("Engine size")
plt.ylabel("Emission")

I don't understand the arguments passed in plt.scatter() and plt.plot() .我不明白在plt.scatter()plt.plot()中传递的 arguments 。 I noticed that when I remove the method plt.plot() , the line of best fit is not plotted on the graph.我注意到当我删除方法plt.plot()时,最佳拟合线不会绘制在图表上。

The plt library plots data. plt 库绘制数据。 The first entry is the x data, the second the y data.第一个条目是 x 数据,第二个条目是 y 数据。 Other inputs can be used to add colour, linewidth or marker type, as seen in the documentation .其他输入可用于添加颜色、线宽或标记类型,如文档中所示。

plt.scatter adds a scatter plot of your data. plt.scatter添加数据的散点图 plot。 Guessing by the name of your variables I suspect:我怀疑你的变量名称猜测:

plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')

Makes a scatter plot of all the engine sizes and their corresponding CO2 emmisions using blue markers.使用蓝色标记制作所有发动机尺寸及其对应的二氧化碳排放量的散点图 plot。

The plt.plot draws a line. plt.plot画了一条线。 Guessing by the name of your variables I suspect我怀疑你的变量名称猜测

plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-y')

Will plot the linear regression of your training data in yellow.将 plot 您的训练数据的线性回归用黄色表示。 train_x is the x data and regr.coef_[0][0]*train_x + regr.intercept_[0] the y data (it follows the formula y = a*x + b). train_x是 x 数据,而regr.coef_[0][0]*train_x + regr.intercept_[0]是 y 数据(它遵循公式 y = a*x + b)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM