Home > database >  Plot two one seaborn plot from two dataframes
Plot two one seaborn plot from two dataframes

Time:09-23

I try to plot two dataframes with seaborn into one figure.

given these test data:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df['Name'] = 'Adam'
df.iloc[::5, 4] = 'Berta'

df.head(10)
    A   B   C   D   Name
0   40  75  45  6   Berta
1   52  98  55  44  Adam
2   57  61  70  17  Adam
3   52  5   20  28  Adam
4   63  53  74  49  Adam
5   53  28  97  26  Berta
6   64  38  73  56  Adam
7   25  65  34  64  Adam
8   95  91  92  60  Adam
9   6   54  5   58  Adam

and

df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df1['Location'] = 'New York'
df1.iloc[::5, 4] = 'Tokyo'
df1.head(10)

   A    B   C   D   Location
0   89  16  23  15  Tokyo
1   7   35  26  21  New York
2   64  94  51  61  New York
3   84  16  15  36  New York
4   55  62  0   2   New York
5   73  93  4   1   Tokyo
6   93  11  27  69  New York
7   14  52  50  45  New York
8   26  77  86  32  New York
9   21  10  68  11  New York

A)The first plot I would like to plot a relplot or scatterplot where both dataframes have the same x and y axes, but a different "hue". If I try:

sb.relplot(data=df, x='Name', y='C', hue="Name", height=8.27, aspect=11.7/8.27)
sb.relplot(data=df1, x='Location', y='C', hue="Location", height=8.27, aspect=11.7/8.27)
plt.show()

The latter plot will overwrite the first or creates a new one. Any ideas?

B) Now we have the same y-axes (let's say "amount"), but with different x-axes (strings). I found this here: enter image description here

As for the 'B' plot, if you would want everything on one plot, then you may try:

fig, ax = plt.subplots(1, 1, figsize=(8, 4))
sb.scatterplot(data=df, x='Name', y='A', hue='Name', palette=['blue', 'orange'], 
               ax=ax)
sb.scatterplot(data=df1, x='Location', y='A', hue='Location', palette=['red', 'green'], 
               ax=ax)
ax.set_xlabel('Name/Location')
plt.show()

Here I made a single subplot and assigned both scatter plots to it. Might require color mapping and renaming X-axis:

enter image description here

  • Related