Seaborn: Using regplot with scatter=False. Not working as intended?

Created on 18 May 2015  路  9Comments  路  Source: mwaskom/seaborn

Hi, I'm trying to draw a regression plot showing just the regression line and confidence interval bands, without the points in the scatter plot, though running the following does not seem to work as I expected.

import seaborn as sb
x = np.linspace(0, 10, 100)
e = np.random.normal(size=100)
y = 1 + 0.5*x + 2*e

g = sb.regplot(x, y, scatter=False)
g.set(xlim=(min(x), max(x)), ylim=(-2, 8))

The following code produces a band that only extends for a small portion of the x and y range.

image

I can work around it by explicitly setting the point colour to white, but I feel there should be a better way to do it.

g = sb.regplot(x, y, scatter_kws={"color": "white"})
g.set(xlim=(min(x), max(x)), ylim=(-2, 8))

image

Have I missed out a parameter I need to do this, or is this a bug?

Most helpful comment

sns.regplot(x="total_bill", y="tip", data=tips, scatter_kws=dict(alpha=0))

All 9 comments

You need to set the limits of the axis before calling regplot.

If we just leave the axis limits as they are, the result is not really what I expect either. But maybe I am missing something?

g = sns.regplot(x, y, scatter=True)

1

g = sns.regplot(x, y, scatter=False)

2

I mean do

f, ax = plt.subplots()
ax.set(xlim=(-2, 12), ylim=(-2, 8))
sns.regplot(x, y, scatter=False)

In that order. regplot tries to avoid messing with the axes it's drawing into, and by default it draws the regression line to the current limits of the axes. That means if you're just drawing a regression line onto an empty axes, it won't be that useful. I think if you do truncate=True it will probably also work; that will set the limits of the regression line to the limits of your data.

Ah...yes, that makes sense.

Great. Anyway, it's working as "intended", if perhaps not how it's expected (but in the service of avoiding magic that might cause annoying problems in other settings).

@mwaskom truncate=True doesn't seem to work as you suggest; and I think it would be very convenient if it did. Is this worth a minor code change?

How exactly do you mean? Do you have an example?

tips = sns.load_dataset("tips")

# everything is fine
sns.regplot(x="total_bill", y="tip", data=tips)

plt.figure()
# the regression line extends only from 18.5 to 21 on the x axis, as described by OP
sns.regplot(x="total_bill", y="tip", data=tips, scatter=False)

# the fix as explained above by @mwaskom works perfectly
# except we have to manually calculate xlim values :(
f, ax= plt.subplots()
ax.set(xlim=(min(tips['total_bill']), max(tips['total_bill'])))
sns.regplot(x="total_bill", y="tip", data=tips, scatter=False)

plt.figure()
# I thought this would also show the full regression line as suggested above but it didn't
sns.regplot(x="total_bill", y="tip", data=tips, scatter=False, truncate=True)

sns.regplot(x="total_bill", y="tip", data=tips, scatter_kws=dict(alpha=0))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sofiatti picture sofiatti  路  4Comments

tritemio picture tritemio  路  3Comments

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

stonebig picture stonebig  路  4Comments

rrbarbosa picture rrbarbosa  路  3Comments