Scatterplot without linear fit in seaborn

杀马特。学长 韩版系。学妹 提交于 2019-12-20 17:42:03

问题


I am wondering if there is a way to turn off the linear fit in seaborn's lmplot or if there is an equivalent function that just produces the scatterplot. Sure, I could also use matplotlib, however, I find the syntax and aesthetics in seaborn quite appealing. E.g,. I want to plot the following plot

import seaborn as sns
sns.set(style="ticks")

df = sns.load_dataset("anscombe")
sns.lmplot("x", "y", data=df, hue='dataset')

Without the linear fit like so:

from itertools import cycle
import numpy as np

import matplotlib.pyplot as plt

color_gen = cycle(('blue', 'lightgreen', 'red', 'purple', 'gray', 'cyan'))

for lab in np.unique(df['dataset']):
    plt.scatter(df.loc[df['dataset'] == lab, 'x'], 
                df.loc[df['dataset'] == lab, 'y'], 
                c=next(color_gen),
                label=lab)

plt.legend(loc='best')


回答1:


set fit_reg argument to False:

sns.lmplot("x", "y", data=df, hue='dataset', fit_reg=False)



回答2:


This doesn't directly answer the question, but may help others who find there way here who just want to do a plain old scatter plot.
As of version 0.9.0 seaborn now has a scatterplot method.

import seaborn as sns
sns.set(style="ticks")

df = sns.load_dataset("anscombe")
sns.scatterplot("x", "y", data=df, hue='dataset')




回答3:


I recommend instead of sns.lmplot() to use sns.scatterplot()

# import libaries
import seaborn as sns

# load tips dataset from GitHub seaborn repository
tips_df = sns.load_dataset("tips")

#create scatter plot
sns.scatterplot(x = "tip", y = "total_bill", data = tips_df, hue ="sex")

To learn more in detail follow seaborn scatter plot using sns.scatterplot() tutorial



来源:https://stackoverflow.com/questions/29637150/scatterplot-without-linear-fit-in-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!