首页 > 解决方案 > 在 seaborn 计数图中标记每种颜色

问题描述

我正在尝试绘制一个具有很长 x 值的 seaborn 计数图,这些值实际上是句子。因此,与其将它们插入 xticks 下,我认为使用 countplot 的色调参数并将标签放在图例中可能会更好。但这会产生彼此相距很远的极细条(参见下面的示例),当我希望这些条成为我没有为色调参数分配任何东西时的样子。我该如何解决?

例子:

df0 = pd.DataFrame({'col':['This is a very long string. So yeah, there you have it, a long, long string.', 
                           'What, you egg? said William Shakespeare.', 
                           'OK, this is now getting out of hand.']})
seaborn.countplot(df0['col'], hue=df0['col'])
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))

此代码产生

在此处输入图像描述

我想要的图表:

在此处输入图像描述

标签: pythonmatplotlibseaborn

解决方案


根据: http ://seaborn.pydata.org/generated/seaborn.countplot.html#seaborn.countplot

您需要添加dodge=False到您的通话中countplot

闪避:布尔,可选

使用色调嵌套时,是否应沿分类轴移动元素。

seaborn.countplot('col', hue='col', dodge=False, data=df0)

推荐阅读