0

Lets say I have a dataframe df which has index 0 to 10, and 4 columns of data, c_1, c_2, c_3, and c_4. When I plot this as follows:

sns.regplot(data=df, x='c_1', y='c_2')
sns.regplot(data=df, x='c_3', y='c_4') 

I get one graph on which there are two regression scatterplots. However, instead, I want one graph with only one regression scatterplot, keeping each indexed c_1 and c_3 as an x paired with c_2 and c_4 as y values, respectively. How can I do this? thanks for any help, let me know if I can make this more clear.

0

3 Answers 3

1

Most seaborn functions work better with long-form data. So, we could reorganize the data and label the categories that you imply with your code. Then, we plot a regplot for all data and plot a scatterplot on top for both categories:

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

#generate sample data
import numpy as np
np.random.seed(123)
n = 20
df = pd.DataFrame({"c_1":np.random.random(n), 
                   "c_2":np.zeros(n), 
                   "c_3":2 * np.random.random(n) - .5, 
                   "c_4":np.zeros(n)})
df.c_2 = 2 * df.c_1 - 3 * np.random.random(n)
df.c_4 = 3 * df.c_3 - 2 * np.random.random(n)        

#reformat the data for plotting...
df_plot = df[["c_1", "c_2"]].copy()
df_plot["cat"] = "set1" 
df_temp = df[["c_3", "c_4"]].copy()
df_temp.columns = ["c_1", "c_2"]
df_temp["cat"] = "set2" 
df_plot = df_plot.append(df_temp)

#...and plot
sns.regplot(data=df_plot, x='c_1', y='c_2', scatter=False)
sns.scatterplot(data=df_plot, x="c_1", y="c_2", hue="cat")

plt.show()

Sample output: enter image description here

You may want to give the regplot a different color to avoid the impression the fitted line just applies to some of the data points in the scatter plot.

Sign up to request clarification or add additional context in comments.

2 Comments

You're getting a weird shadowing effect that I think will go away if you add scatter=False to the regplot call.
Excellent suggestion. When I first noticed it, I thought about increasing the marker size in the scatter plot (I did not know that you can turn the scatter plot off in regplot) but decided against it because, well, I like this effect. But obviously not everybody's cuppa, so I changed it.
1

You can use subplots for that.

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

plt.figure(figsize=(12,5))

#subplot(nrows, ncols, index)
plt.subplot(1, 2, 1)
sns.regplot(data=df, x='c_1', y='c_2')

plt.subplot(1, 2, 2)
sns.regplot(data=df, x='c_3', y='c_4') 

plt.show()

enter image description here

Comments

0

try a jointplot

 np.random.seed(123)
 n = 20
 df = pd.DataFrame({"c_1":np.random.random(n), 
               "c_2":np.zeros(n), 
               "c_3":2 * np.random.random(n) - .5, 
               "c_4":np.zeros(n)})
 df.c_2 = 2 * df.c_1 - 3 * np.random.random(n)
 df.c_4 = 3 * df.c_3 - 2 * np.random.random(n)        

 #reformat the data for plotting...
 df_plot = df[["c_1", "c_2"]].copy()
 df_plot["cat"] = "set1" 
 df_temp = df[["c_3", "c_4"]].copy()
 df_temp.columns = ["c_1", "c_2"]
 df_temp["cat"] = "set2" 
 df_plot = df_plot.append(df_temp)

 import scipy.stats as stats

 plt.title('combined sets')
 g=sns.JointGrid(data=df_plot, x='c_1', y='c_2')
 g.plot( sns.regplot,sns.scatterplot)
 g=g.plot_joint(sns.kdeplot)
 g=g.plot_marginals(sns.kdeplot,shade=True)
 plt.show()


 filter=df_plot['cat']=='set1'
 plt.title('Set1')
 g = (sns.jointplot(x="c_1",
         y="c_2",
         kind='scatter',
         color='green',
         data=df_plot[filter],
         marginal_kws=dict(bins=10, rug=True))
.plot_joint(sns.regplot)
.plot_joint(sns.kdeplot))
 plt.show()

filter=df_plot['cat']=='set2'
plt.title('Set2')
g = (sns.jointplot(x="c_1",
         y="c_2",
         kind='scatter',
         color='blue',
         data=df_plot[filter],
         marginal_kws=dict(bins=10, rug=True))
.plot_joint(sns.regplot)
.plot_joint(sns.kdeplot))

plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.