Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended regression lines with seaborn regplot

I searched but I did not find the answer regrading the seaborn library. I also checked the documentation for lmplot() and regplot(), but did not find either. Is it possible to extend and control the length of regression lines? By default seaborn fits the length of regression line according to the length of x axis. Another option is to use argument truncate=True - that would limit the regression line only to the extent of data. Other options?

In my example I want the lower regression line to be extended down till x=0. And the upper line extended till the intersection with the lower one.

example

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

file = 'cobbles.csv'
df = pd.read_csv(file, sep=',')

sns.regplot(x='downward_temp', y='downward_heat', data=df, ci=None)
sns.regplot(x='upward_temp', y='upward_heat', data=df, ci=None, order=2)


plt.xlim([0,25])
plt.ylim([0,100])
plt.show()
like image 802
Kārlis Rieksts Avatar asked Apr 10 '17 10:04

Kārlis Rieksts


People also ask

What is the use of Seaborn Regplot () method?

regplot() : This method is used to plot data and a linear regression model fit. There are a number of mutually exclusive options for estimating the regression model.

What is the difference between Lmplot and Regplot?

The two functions that can be used to visualize a linear fit are regplot() and lmplot() . These functions draw similar plots, but :func:regplot` is an axes-level function, and lmplot() is a figure-level function.

Which parameter of the Seaborn method Lmplot () allows for wrapping column variables into multiple rows?

col_wrap : (optional) This parameter is of int type, “Wrap” the column variable at this width, so that the column facets span multiple rows.

What does SNS Implot () perform in Seaborn Library?

The lineplot (lmplot) is one of the most basic plots. It shows a line on a 2 dimensional plane. You can plot it with seaborn or matlotlib depending on your preference. The examples below use seaborn to create the plots, but matplotlib to show.


2 Answers

If you know your x limits prior to plotting, you can set_xlim for the axis before calling regplot and seaborn will then extend the regression line and the CI over the range of xlim.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

file = 'cobbles.csv'
df = pd.read_csv(file, sep=',')

fig, ax = plt.subplots()

xlim = [0,25]
ax.set_xlim(xlim)

sns.regplot(x='downward_temp', y='downward_heat', data=df, ci=None, ax=ax)
sns.regplot(x='upward_temp', y='upward_heat', data=df, ci=None, order=2, ax=ax)

ax.set_ylim([0,100])
plt.show()
like image 76
Jake Dunlea Avatar answered Nov 17 '22 18:11

Jake Dunlea


Short answer: You just have to add plt.xlim(start,end) before your Seaborn plots.


I guess it might make more sense for Seaborn to automatically determine the length from the plot limits.

The same issue brought me here, and @Serenity's answer inspired me that something like xlims = ax.get_xlim() might help.

May try fixing and commit a change to Seaborn afterwards.

like image 26
Claire Avatar answered Nov 17 '22 17:11

Claire