Python – seaborn.regplot() 方法
Seaborn是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。 Seaborn 帮助解决了 Matplotlib 面临的两大问题;问题是什么?
- 默认 Matplotlib 参数
- 使用数据框
随着 Seaborn 对 Matplotlib 的补充和扩展,学习曲线非常缓慢。如果您了解 Matplotlib,那么您已经完成了 Seaborn 的一半。
seaborn.regplot() :
此方法用于绘制数据和线性回归模型拟合。有许多相互排斥的选项可用于估计回归模型。欲了解更多信息,请点击此处。
Syntax : seaborn.regplot( x, y, data=None, x_estimator=None, x_bins=None, x_ci=’ci’, scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker=’o’, scatter_kws=None, line_kws=None, ax=None)
Parameters: The description of some main parameters are given below:
- x, y: These are Input variables. If strings, these should correspond with column names in “data”. When pandas objects are used, axes will be labeled with the series name.
- data: This is dataframe where each column is a variable and each row is an observation.
- lowess: (optional) This parameter take boolean value. If “True”, use “statsmodels” to estimate a nonparametric lowess model (locally weighted linear regression).
- color: (optional) Color to apply to all plot elements.
- marker: (optional) Marker to use for the scatterplot glyphs.
Return: The Axes object containing the plot.
下面是上述方法的实现:
示例 1:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("mpg")
# draw regplot
sns.regplot(x = "mpg",
y = "acceleration",
data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("titanic")
# draw regplot
sns.regplot(x = "age",
y = "fare",
data = data,
dropna = True)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("exercise")
# draw regplot
sns.regplot(x = "id",
y = "pulse",
data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("attention")
# draw regplot
sns.regplot(x = "solutions",
y = "score",
data = data)
# show ther plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出 :
示例 2:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("titanic")
# draw regplot
sns.regplot(x = "age",
y = "fare",
data = data,
dropna = True)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出 :
示例 3:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("exercise")
# draw regplot
sns.regplot(x = "id",
y = "pulse",
data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出:
示例 4:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("attention")
# draw regplot
sns.regplot(x = "solutions",
y = "score",
data = data)
# show ther plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出 :