Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatterplot without linear fit in seaborn

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')

enter image description here

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')

enter image description here


2 Answers

set fit_reg argument to False:

sns.lmplot("x", "y", data=df, hue='dataset', fit_reg=False)
like image 190
HYRY Avatar answered Sep 14 '25 15:09

HYRY


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')

enter image description here

like image 45
Michael Hall Avatar answered Sep 14 '25 15:09

Michael Hall