gpt4 book ai didi

python - 在 seaborn stripplot 中添加色相类别标签

转载 作者:行者123 更新时间:2023-11-28 18:02:53 26 4
gpt4 key购买 nike

我有两个 DataFrame,我正在将它们绘制为 strip 图。我几乎可以随心所欲地绘制它们,但我想知道是否可以为“色调”添加类别标签。

目前的剧情是这样的:

Example plot.

但是,我想为每个字母的每个“列”添加类别标签(只有两个)。所以它看起来像这样:

enter image description here

DataFrame 看起来像这样(尽管这些只是编辑过的片段):

     Case Letter Size Weight
0 upper A 20 bold
1 upper A 23 bold
2 lower A 61 bold
3 lower A 62 bold
4 upper A 78 bold
5 upper A 95 bold
6 upper B 23 bold
7 upper B 40 bold
8 lower B 47 bold
9 upper B 59 bold
10 upper B 61 bold
11 upper B 99 bold
12 lower C 23 bold
13 upper D 23 bold
14 upper D 66 bold
15 lower D 99 bold
16 upper E 5 bold
17 upper E 20 bold
18 upper E 21 bold
19 upper E 22 bold

...和...

     Case Letter Size  Weight
0 upper A 4 normal
1 upper A 6 normal
2 upper A 7 normal
3 upper A 8 normal
4 upper A 9 normal
5 upper A 12 normal
6 upper A 25 normal
7 upper A 26 normal
8 upper A 38 normal
9 upper A 42 normal
10 lower A 43 normal
11 lower A 57 normal
12 lower A 90 normal
13 upper B 4 normal
14 lower B 6 normal
15 upper B 8 normal
16 upper B 9 normal
17 upper B 12 normal
18 upper B 21 normal
19 lower B 25 normal

我的相关代码是:

fig, ax = plt.subplots(figsize=(10, 7.5))
plt.tight_layout()

sns.stripplot(x=new_df_normal['Letter'], y=new_df_normal['Size'],
hue=new_df_normal['Case'], jitter=False, dodge=True,
size=8, ax=ax, marker='D',
palette={'upper': 'red', 'lower': 'red'})

plt.setp(ax.get_legend().get_texts(), fontsize='16') # for legend text
plt.setp(ax.get_legend().get_title(), fontsize='18') # for legend title

ax.set_xlabel("Letter", fontsize=20)
ax.set_ylabel("Size", fontsize=20)
ax.set_ylim(0, 105)
ax.tick_params(labelsize=20)

ax2 = ax.twinx()

sns.stripplot(x=new_df_bold['Letter'], y=new_df_bold['Size'],
hue=new_df_bold['Case'], jitter=False, dodge=True,
size=8, ax=ax2, marker='D',
palette={'upper': 'green', 'lower': 'green'})

ax.legend_.remove()
ax2.legend_.remove()

ax2.set_xlabel("", fontsize=20)
ax2.set_ylabel("", fontsize=20)
ax2.set_ylim(0, 105)
ax2.tick_params(labelsize=20)

是否可以为每一列添加这些类别标签(“粗体”和“正常”)?

最佳答案

使用 seaborn 的散点图,您可以访问 style(甚至 size)参数。但是您最终可能不会得到您想要的布局。 scatterplot documentation .

或者您可以使用 catplot 并处理行和列。 seaborn doc for catplot

不幸的是,Seaborn 本身并没有提供您正在寻找的东西:在 stripplot 中的 hue 参数之外的另一层嵌套(参见 stripplot documentation。一些 seaborn 门票已打开这可能是相关的,例如 this ticket 。但是我在 seaborn 中遇到了一些类似的功能请求,但被拒绝了,请参见 this ticket

最后一种可能性是深入研究 matplotlib 基元来操纵您的 seaborn 图(因为 seaborn 就在 matplotlib 之上)。不用说,这需要付出很多努力,并且最终可能首先使 seaborn 无效 ;)

关于python - 在 seaborn stripplot 中添加色相类别标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com