Extended regression lines with maritime register

I have searched but I have not found an answer fixing the nautical library. I also checked the documentation for lmplot()

and regplot()

but couldn't find it. Is it possible to extend and control the length of the regression lines? By default, the sea wave matches the length of the regression line according to the length of the x-axis. Another option is to use an argument truncate=True

- which would limit the regression line to just the amount of data. Other options?

In my example, I want the bottom regression line to extend to x = 0. And the top line to extend to intersect with the bottom.

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

      

+3


source to share


3 answers


You should use a scipy.stats.linregress

linear regression function such as seaborn do to calculate. Then you need to create an x ​​array to cover the new x-axis boundaries of the canvas and plot on its extended regression line. See the example for more details:

import numpy as np; np.random.seed(8)

import seaborn as sns
import matplotlib.pylab as plt
import scipy.stats

# test data
mean, cov = [4, 6], [(1.5, .7), (.7, 1)]
x, y = np.random.multivariate_normal(mean, cov, 80).T
ax = sns.regplot(x=x, y=y, color="g")

# extend the canvas
plt.xlim([0,20])
plt.ylim([0,15])

# calculate linear regression function
slope, intercept, r_value, p_value, std_err = \
 scipy.stats.linregress(x=x,y=y)

# plot the regression line on the extended canvas
xlims = ax.get_xlim()
new_x = np.arange(xlims[0], xlims[1],(xlims[1]-xlims[0])/250.)
ax.plot(new_x, intercept + slope *  new_x, color='g', linestyle='-', lw = 2.5)

plt.show()

      



enter image description here

+1


source


If you know your x limits before plotting, you can set_xlim

for the axis before the call regplot

, and then the sea wave will continue the regression line and CI in the xlim range.



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

      

+1


source


Short answer: you just need to add plt.xlim(start,end)

Seaborn to your graphs.


I think it might make more sense for Seaborn to automatically determine the length from within the graph.

The same question led me here and @ Serenity's answer inspired me that something like this might help xlims = ax.get_xlim()

.

May try to commit and commit changes to Seaborn afterwards.

0


source







All Articles