[英]How can I enumerate and add margin_titles to each subplot in a seaborn lmplot facetgrid?
首先,我想简化每个子图的标题,只有语料库 = {语料库名称}。
我按照 lmplot 生成这些图
g=sns.lmplot('x', 'y', data=test_plot, col='corpus', hue = 'monotonicity', row='measure', sharey=True, sharex=True, height=2.5,aspect=1.25, truncate=False, scatter_kws={"marker": "D", "s": 20})
g=(g.set_axis_labels("Max-Min (measure)", "Max-Min (comp measure)")
.set(xlim=(0, 1), ylim=(-.1, 1))
.fig.subplots_adjust(wspace=.02))
我想使用 facetgrid margin_title
选项将度量值放在右侧的 y 轴上,但是 get lmplot() got an unexpected keyword argument 'margin_titles'
然后我尝试使用 facetgrid,如下所示:
p = sns.FacetGrid(data = test_plot,
col = 'corpus',
hue = 'monotonicity',
row = 'measure',
margin_titles=True)
p.map(sns.lmplot, 'diff_', 'score_diff', data=test_plot, he='monotonicity', truncate=False, scatter_kws={"marker": "D", "s": 20})
但是后来我收到了一个关于lmplot() got an unexpected keyword argument 'color'
的错误lmplot() got an unexpected keyword argument 'color'
(无法弄清楚为什么会抛出它?)。
我的第二个问题是我想在每个子图的标题中添加一个字母/枚举,如(a), ..., (i)
,但对于我的生活,我无法弄清楚如何做到这一点。
因为你的定制需求,考虑运行后,你通过所有的FacetGrid的轴迭代lmplot
。 关于您的特定错误, seaborn.lmplot
是一个 FacetGrid,因此如果在第二次尝试中尝试嵌套在另一个 FacetGrid 中,则会发生冲突。 此外,在下面的解决方案中,不要将g
重新分配给返回NoneType
轴设置:
#... SAME lmplot ...
(
g.set_axis_labels("Max-Min (measure)", "Max-Min (comp measure)")
.set(xlim=(0, 1), ylim=(-.1, 1))
.fig.subplots_adjust(wspace=.02)
)
alpha = list('abcdefghijklmnopqrstuvwxyz')
axes = g.axes.flatten()
# ADJUST ALL AXES TITLES
for ax, letter in zip(axes, alpha[:len(axes)]):
ttl = ax.get_title().split("|")[1].strip() # GET CURRENT TITLE
ax.set_title(f"({letter}) {ttl}") # SET NEW TITLE
# ADJUST SELECT AXES Y LABELS
for i, m in zip(range(0, len(axes), 3), test_plot["measure"].unique()):
axes[i].set_ylabel(m)
输入(用于演示的纯随机数据)
import numpy as np
import pandas as pd
np.random.seed(1172021)
test_plot = pd.DataFrame({
'measure': np.random.choice(["precision", "recall", "F1-score"], 500),
'corpus': np.random.choice(["Fairview", "i2b2", "MiPACQ"], 500),
'monotonicity': np.random.choice(["increasing", "non", "decreasing"], 500),
'x': np.random.uniform(0, 1, 500),
'y': np.random.uniform(0, 1, 500)
})
输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.