我有一个散点图,其中包含使用 seaborn 的“col”和“row”功能的多个子图,例如
sns.relplot(data=data,x="YEL-HLog",y="FSC-HLog",hue="Treatment",row="Fraction",col="Biounit",s=1)
我想在每个子图上的参数 x 上覆盖一条线。关键是该行因不同的列而异。在这种情况下,我使用了以下代码:
sns.relplot(data=new,x="Threshold",y="FSC-HLog",hue="Treatment",row="Fraction",col="Biounit",s=1)
“新建”是相同的数据框,但插入了“阈值”列。所以一切都一样,除了 x 值。
然而,这只是给了我两个不同的图表。如何将两者结合以显示在同一个图上?
答案 0 :(得分:2)
每次调用图形级函数(例如 sns.relplot
)时,都会创建一个新图形。 template<std::unsigned_integral xT,
std::unsigned_integral yT,
std::unsigned_integral paletteT,
std::unsigned_integral texture_idT>
[[nodiscard]] Color16
get_color(const xT x,
const yT y,
const BPPT depth,
const paletteT palette = 0U,
const texture_idT texture_id = 0U) const
返回包含如何创建子图的信息的 relplot
。您可以遍历 FacetGrid
并在每一个上画一条线。
这是一个例子:
g.axes
目前还不清楚 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()
数据框如何获取其值。它可以从 new
创建,但这似乎是不必要的间接。为了完整起见,在这种情况下,代码可能如下所示:
threshold_dict