python 带数据分布的线性回归最佳拟合线的散点图绘制步骤

该博客探讨了如何利用Seaborn库中的jointplot()函数绘制数据的最佳拟合线,以理解两个变量之间的相互变化关系。示例代码展示了如何加载和调整参数,创建一个线性回归的联合分布图,并提供了自定义样式和颜色的选择。此外,还提到了数据预处理技巧,如将多维数组转换为一维数组。
摘要由CSDN通过智能技术生成

**目的:**如果想了解两个变量如何相互改变,那么确定最佳拟合线就是常用的方法。下图显示了数据中各组之间最佳拟合线的差异。每个轴拟合了各自数据的分布。

示例参考代码:

import seaborn as sns
sns.set(style="darkgrid")
##### 加载数据
tips = sns.load_dataset("tips")
##### jointplot 调整对应的参数
g = sns.jointplot("total_bill", "tip", 
data=tips,kind="reg", truncate=False,
xlim=(0, 60), ylim=(0, 12),
color="m", height=7)

通过设置 kind=“reg” ,可以设置线性回归的拟合分布。(类似regplot())
在这里插入图片描述
可自己设置sns.jointplot()里相应的参数,改变plot的风格样式。参见
https://seaborn.pydata.org/generated/seaborn.jointplot.html

设置参数的细节:

Parameters:
x, y vectors or keys in data
Variables that specify positions on the x and y axes.

datapandas.DataFrame, numpy.ndarray, mapping, or sequence
Input data structure. Either a long-form collection of vectors that can be assigned to named variables or a wide-form dataset that will be internally reshaped.

kind{ “scatter” | “kde” | “hist” |hex| “reg” | “resid” }

Kind of plot to draw. See the examples for references to the underlying functions.

colormatplotlib color
Single color specification for when hue mapping is not used. Otherwise, the plot will try to hook into the matplotlib property cycle.

heightnumeric
Size of the figure (it will be square).

rationumeric
Ratio of joint axes height to marginal axes height.

spacenumeric
Space between the joint and marginal axes

dropnabool
If True, remove observations that are missing from x and y.

{x, y}limpairs of numbers
Axis limits to set before plotting.

marginal_ticksbool
If False, suppress ticks on the count/density axis of the marginal plots.

{joint, marginal}_kwsdicts
Additional keyword arguments for the plot components.

huevector or key in data
Semantic variable that is mapped to determine the color of plot elements. Semantic variable that is mapped to determine the color of plot elements.

palettestring, list, dict, or matplotlib.colors.Colormap
Method for choosing the colors to use when mapping the hue semantic. String values are passed to color_palette(). List or dict values imply categorical mapping, while a colormap object implies numeric mapping.

hue_ordervector of strings
Specify the order of processing and plotting for categorical levels of the hue semantic.

hue_normtuple or matplotlib.colors.Normalize
Either a pair of values that set the normalization range in data units or an object that will map from data units into a [0, 1] interval. Usage implies numeric mapping.

kwargs
Additional keyword arguments are passed to the function used to draw the plot on the joint Axes, superseding items in the joint_kws dictionary.

加载数据时遇到的小tips: 把多维数组变一维数组的方法(当加载.mat文件时候,默认加载的是一列数据,可以用flatten函数将多维数组变成一维数组)

:a = a.flatten()
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

℡folk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值