Seaborn:设置catplot条的厚度

2024-06-28 20:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想增加catplot中钢筋的厚度,并制作y轴日志。可复制示例:

import seaborn as sns
df = sns.load_dataset("titanic")

p = sns.catplot(x="alive", hue="alive", col="deck",
                data=df, kind="count",
                col_wrap = 4 )
plt.legend(title = 'alive',bbox_to_anchor=(1.05, 1), 
           loc=2, borderaxespad=0.)

catplot thin bars


Tags: import示例dfasloadcolseabornhue
1条回答
网友
1楼 · 发布于 2024-06-28 20:27:15

如果同时使用xhue两个可能的值,seaborn将为每个子批次提供4条钢筋:否,否,是,否,是。在这种情况下,其中两个是空的。解决方案不是设置显式的hue

然后,可以从Xticklabel和xlabel作为标题创建图例。在网格中,XTICK通常只显示在最后一行,因此我们可以使用标签的最后一个子批次

因为现在信息在图例中,所以可以删除XTICK和xlabel

示例如下所示:

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset("titanic")
p = sns.catplot(x="alive", col="deck",
                data=df, kind="count",
                col_wrap=4)

# create legend from the patches (for the colors) and xticklabels (for the text)
plt.legend(p.axes[0].patches,
           [l.get_text() for l in p.axes[-1].get_xticklabels()],
           title=p.axes[-1].get_xlabel(),
           bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

# remove xticks because they now are in the legend
for ax in p.axes:
    ax.set_xticks([])
    ax.set_xlabel('')
plt.show()

example plot

相关问题 更多 >