My challenge is to overlay a custom line function graph over a scatter plot I already have, the code looks like follows:
base_beta = results.params
X_plot = np.linspace(0,1,400)
g = sns.FacetGrid(data, size = 6)
g = g.map(plt.scatter, "usable_area", "price", edgecolor="w")
Where base_beta is only a constant, and then one coefficient. Basically, I want to overlay a function that plots a line y = constant + coefficient * x
I tried to overlay a line using this but it did not work.
g = g.map_dataframe(plt.plot, X_plot, X_plot*base_beta[1]+base_beta[0], 'r-')
plt.show()
The current scatter plot looks like so:
Can any one help me with this?
--ATTEMPT 1
base_beta = results.params
X_plot = np.linspace(0,1,400)
Y_plot = base_beta [0] + base_beta[1]*X_plot
g = sns.FacetGrid(data, size = 6)
g = g.map(plt.scatter, "usable_area", "price", edgecolor="w")
plt.plot(X_plot, Y_plot, color='r')
plt.show()
Resulted in the same graph but no line:

Seaborn's refline() function to add horizontal/vertical lines in subplots. To add a horizontal and vertical line we can use Seaborn's refline() function with x and y y co-ordinates for the locations of the horizontal and vertical lines.
You can just call plt.plot to plot a line over the data.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
data = pd.DataFrame()
data['usable_area'] = 5*np.random.random(200)
data['price'] = 10*data['usable_area']+10*np.random.random(200)
X_plot = np.linspace(0, 7, 100)
Y_plot = 10*X_plot+5
g = sns.FacetGrid(data, size = 6)
g = g.map(plt.scatter, "usable_area", "price", edgecolor="w")
plt.plot(X_plot, Y_plot, color='r')
plt.show()
Produces:

seaborn.relplot or seaborn.regplot instead of directly using seaborn.FacetGrid
python 3.8.12, pandas 1.3.3, matplotlib 3.4.3, seaborn 0.11.2import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# create a dataframe with sample x and y
np.random.seed(365)
x = 5*np.random.random(200)
df = pd.DataFrame({'x': x, 'y': 10*x+10*np.random.random(200)})
# add custom line to the dataframe
base_beta = [10, 5]
df['y_line'] = base_beta[0] + base_beta[1]*df.x
display(df.head())
x y y_line
0 4.707279 50.634968 33.536394
1 3.208014 33.890507 26.040068
2 3.423052 37.853276 27.115262
3 2.942810 29.899257 24.714052
4 2.719436 36.932170 23.597180
sns.relplot with .map or .map_dataframe
sns.lineplot) to each facet of the figure-level plot.p1 = sns.relplot(kind='scatter', x='x', y='y', data=df, height=3.5, aspect=1.5)
p1.map_dataframe(sns.lineplot, 'x', 'y_line', color='g')

sns.scatterplot with sns.lineplot
fig, ax = plt.subplots(figsize=(6, 4))
p1 = sns.scatterplot(data=df, x='x', y='y', ax=ax)
p2 = sns.lineplot(data=df, x='x', y='y_line', color='g', ax=ax)

seaborn.lmplot for figure-level regression plotseaborn.regplot for an axes-level regression plot.sns.lmplotp1 = sns.lmplot(data=df, x='x', y='y', line_kws={'color': 'g'}, height=3.5, aspect=1.5)

sns.regplotp2 = sns.regplot(data=df, x='x', y='y', line_kws={'color': 'g'})

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