用多个子图在 seaborn 散点图上叠加一条垂直线 [英] Overlay a vertical line on seaborn scatterplot with multiple subplots

查看:69
本文介绍了用多个子图在 seaborn 散点图上叠加一条垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个散点图,其中包含使用 seaborn 的col"的多个子图;和行"特征,例如

sns.relplot(data=data,x=YEL-HLog",y=FSC-HLog",hue=治疗",row=分数",col=";生物单位",s=1)

我想在每个子图上的参数 x 上覆盖一条线.关键是该行因不同的列而异.在这种情况下,我使用了以下代码:

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

新"是相同的数据帧,但具有阈值"列插入.所以一切都一样,除了 x 值.

然而,这只是给了我两个不同的图表.如何将两者结合以显示在同一个图上?

解决方案

每次一个图形级函数比如

目前还不清楚 new 数据框如何获取其值.它可以从 threshold_dict 创建,但这似乎是不必要的间接.为了完整起见,在这种情况下,代码可能如下所示:

new_df = 数据new_df[阈值"] = data.apply(lambda d: threshold_dict[(d['Fraction'], d['Biounit'])],axis=1)为了 ...为了 ...阈值 = new_df[(new_df[分数"] == row_name) &(new_df[Biounit"] == col_name)][阈值"].iloc[0]

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?

解决方案

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()

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]

这篇关于用多个子图在 seaborn 散点图上叠加一条垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆