0

I saw several answers regarding subplots but I am unsure how to set it up using my code. I have two functions that give me two lmplots from Seaborn and I would like to get the figure shown below. How can I make the two lmplots look like the other figure which was obtained using R.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Martin2015_df = pd.read_csv('Martin2015_data.csv',
                            sep= '\t', 
                            on_bad_lines= 'warn', 
                            na_values=" "
                           ) #Creates the dataframe from the file

Martin2015_df.shape #Allows to see if the file -> dataframe code works
def R_plotA(): #Defining the function
    """ Replicates the scatter plot of daily nest predation 
    rate and compares it to the growth rate from the Martin2015_data.csv
    file. It then saves that image to what the current working directory is.
    """
colors = sns.color_palette(["#F88078", "#00BA38", "#619CFF"]) # Selecting color to be used

RPA = sns.lmplot(data=Martin2015_df,
                x="nstldpr",
                y="krate",
                hue="site",
                palette=colors,
                markers=[".", "^", "s"],
                ci=None,
                height=6,
                facet_kws={'legend_out': True}
               ) #Creates the figure

RPA.set(xlim = (0.000, 0.1000), ylim = (0.25, 0.600)) #Sets the axes limits

plt.title("A", x = 0) #Adds a title
plt.ylabel("Growth rate") #Adds a Y label
plt.xlabel("Daily nest predation rate") #Adds an X label

sns.move_legend(RPA, loc = "lower center", bbox_to_anchor = (0.5, -0.05), ncol = 4) #Adjusts the legend
# title
new_title = 'site'
RPA._legend.set_title(new_title)
# replace labels
new_labels = ['Arizona - temperate', 'Venezuela - tropical', 'Malaysia - tropical']
for t, l in zip(RPA._legend.texts, new_labels):
    t.set_text(l)

plt.grid(axis = "both") #Adds apropriate gridlines

plt.savefig("Martin_FigureA.png", orientation = "portrait", bbox_inches = "tight") #Saves figure

R_plotA()
plt.show()

The image show is a result of R_plotA()

def R_plotB(): #Defining the function
    """ Replicates the scatter plot of daily nest predation 
    rate and compares it to the growth rate from the Martin2015_data.csv
    file. It then saves that image to what the current working directory is.
    """
colors = sns.color_palette(["#F88078", "#00BA38", "#619CFF"]) # Selecting color to be used

RPB = sns.lmplot(data=Martin2015_df,
                x="krate",
                y="nstl",
                hue="site",
                palette=colors,
                markers=[".", "^", "s"],
                ci=None,
                height=6,
                facet_kws={'legend_out': True}
               ) #Creates the figure

RPB.set(xlim = (0.220, 0.600), ylim = (7, 30)) #Sets the axes limits

plt.title("B", x = 0) #Adds a title
plt.ylabel("Nestling period") #Adds a Y label
plt.xlabel("Growth rate") #Adds an X label

sns.move_legend(RPB, loc = "lower center", bbox_to_anchor = (0.5, -0.05), ncol = 4) #Adjusts the legend
# title
new_title = 'site'
RPB._legend.set_title(new_title)

# replace labels
new_labels = ['Arizona - temperate', 'Venezuela - tropical', 'Malaysia - tropical']
for t, l in zip(RPB._legend.texts, new_labels):
    t.set_text(l)

plt.grid(axis = "both") #Adds apropriate gridlines

plt.savefig("Martin_FigureB.png", orientation = "portrait", bbox_inches = "tight") #Saves figure

R_plotB()
plt.show()

The followinf image is a rsult from R_plotB

How can i make the above images look like this figure combined

Also how do I make "site" appear on the left of the legend rather then on top.

Thank you!

  • 1
    _**I am looking to have two Seaborn lmplots side by side**_ this is not possible because `lmplot` is a `FacetGrid`. Instead, use `regplot` with `fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 7))`. `sns.regplot(..., ax=ax1)` and `sns.regplots(..., ax=ax2)` – Trenton McKinney Dec 12 '22 at 20:31

0 Answers0