ong*_*enz 5 python matplotlib pandas seaborn
我有两个数据框,我将它们绘制为条形图。我几乎可以按照自己的意愿绘制它们,但我想知道是否可以为“色调”添加类别标签。
目前的剧情是这样的:
但是,我想将类别标签(只有两个)添加到每个字母的每个“列”中。所以它看起来像这样:
数据帧看起来像这样(尽管这些只是编辑过的片段):
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
Run Code Online (Sandbox Code Playgroud)
...和...
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
Run Code Online (Sandbox Code Playgroud)
我的相关代码是:
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)
Run Code Online (Sandbox Code Playgroud)
是否可以为每列添加这些类别标签(“粗体”和“正常”)?
使用seaborn\xe2\x80\x99s散点图,您可以访问style(甚至size)参数。但最终您可能不会得到您想要的布局。散点图文档。
或者您可以使用catplot并玩转行和列。catplot 的 Seaborn 文档
不幸的是,Seaborn 本身并没有提供您正在寻找的内容:超出hue参数的另一层嵌套stripplot(请参阅stripplot 文档。打开了一些可能相关的 Seaborn 票证,例如这张票证。但是我\xe2\x80\x99ve 遇到了一些Seaborn 中被拒绝的类似功能请求,请参阅此票证
最后一种可能性是深入研究 matplotlib 原语来操作seaborn图(因为seaborn就在matplotlib之上)。不用说,这需要付出很多努力,并且可能最终会导致 Seaborn 失效;)
\n