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.
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()
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.
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.
col_wrap : (optional) This parameter is of int type, “Wrap” the column variable at this width, so that the column facets span multiple rows.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With