Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Overlay a vertical line on seaborn scatterplot with multiple subplots

I have a scatterplot that consists of multiple subplots using seaborn's "col" and "row" feature, e.g.

sns.relplot(data=data,x="YEL-HLog",y="FSC-HLog",hue="Treatment",row="Fraction",col="Biounit",s=1)

I want to overlay a line over parameter x on each of those subplots. The kicker is that line differs for different columns. To that extent, I used the follwoing code:

sns.relplot(data=new,x="Threshold",y="FSC-HLog",hue="Treatment",row="Fraction",col="Biounit",s=1)

"New" is the same dataframe but with the column "Threshold" inserted. So everything is the same, except the x value.

However, this just gives me two different graphs. How do I combine the two to show on the same plot?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Every time a figure-level function such as sns.relplot is called, a new figure is created. relplot returns a FacetGrid containing the information of how the subplots are created. You can loop through g.axes and draw a line on each of them.

Here is an example:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

N = 2000
data = pd.DataFrame({"YEL-HLog": np.random.rand(N),
                     "FSC-HLog": np.random.rand(N),
                     "Treatment": np.random.choice(['A', 'B', 'C'], N),
                     "Fraction": np.random.choice(['Fr1', 'Fr2'], N),
                     "Biounit": np.random.choice(['Unit1', 'Unit2', 'Unit3'], N)})
threshold_dict = {('Fr1', 'Unit1'): 0.1, ('Fr1', 'Unit2'): 0.2, ('Fr1', 'Unit3'): 0.3,
                  ('Fr2', 'Unit1'): 0.6, ('Fr2', 'Unit2'): 0.7, ('Fr2', 'Unit3'): 0.8}

g = sns.relplot(data=data, x="YEL-HLog", y="FSC-HLog", hue="Treatment", row="Fraction", col="Biounit", height=3)
for row, row_name in enumerate(g.row_names):
    for col, col_name in enumerate(g.col_names):
        ax = g.axes[row, col]
        threshold = threshold_dict[(row_name, col_name)]
        ax.axvline(threshold, color='red', ls='--', lw=3)
g.fig.subplots_adjust(left=0.07, bottom=0.09)
plt.show()

sns.relplot with vertical line

It is quite unclear how the new dataframe gets its values. It could be created from the threshold_dict, but that seems like an unnecessary indirection. Just to be complete, in that case the code could look as follows:

new_df = data
new_df["Threshold"] = data.apply(lambda d: threshold_dict[(d['Fraction'], d['Biounit'])], axis=1)
for ...
   for ...
        threshold = new_df[(new_df["Fraction"] == row_name) & (new_df["Biounit"] == col_name)]["Threshold"].iloc[0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...