简体   繁体   English

如何枚举和添加 margin_titles 到 seaborn lmplot facetgrid 中的每个子图?

[英]How can I enumerate and add margin_titles to each subplot in a seaborn lmplot facetgrid?

I have the following attached lmplot facetgrid我有以下附加的 lmplot facetgrid lmplot facetgrid

To start with, I want to simplify the title of each subplot, to only have corpus = {corpus name}.首先,我想简化每个子图的标题,只有语料库 = {语料库名称}。

I am generating these plots using the lmplot as per我按照 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))

I want to use the facetgrid margin_title option to put the measure value on the right y-axis, but get lmplot() got an unexpected keyword argument 'margin_titles'我想使用 facetgrid margin_title选项将度量值放在右侧的 y 轴上,但是 get lmplot() got an unexpected keyword argument 'margin_titles'

I then tried using a facetgrid, as per:然后我尝试使用 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})

but then I get an error about lmplot() got an unexpected keyword argument 'color' (cannot figure out why that is being thrown?).但是后来我收到了一个关于lmplot() got an unexpected keyword argument 'color'的错误lmplot() got an unexpected keyword argument 'color' (无法弄清楚为什么会抛出它?)。

My second problem is that I want to add a letter/enumeration to each subplot's title, as in (a), ..., (i) , but for the life of me cannot figure out how to do this.我的第二个问题是我想在每个子图的标题中添加一个字母/枚举,如(a), ..., (i) ,但对于我的生活,我无法弄清楚如何做到这一点。

Because of your custom needs, consider iterating through all the axes of the FacetGrid after running your lmplot .因为你的定制需求,考虑运行后,你通过所有的FacetGrid的轴迭代lmplot Regarding your specific error, seaborn.lmplot is a FacetGrid so will conflict if nested in another FacetGrid as tried in your second attempt.关于您的特定错误, seaborn.lmplot是一个 FacetGrid,因此如果在第二次尝试中尝试嵌套在另一个 FacetGrid 中,则会发生冲突。 Also, in below solution, do not re-assign g to axes setup which returns NoneType :此外,在下面的解决方案中,不要将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)

Input (purely random data for demonstration)输入(用于演示的纯随机数据)

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

Output输出

FacetGrid 绘图输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM