带有pandas/matplotlib或seaborn的排序条形图

ana*_*nuc 8 python matplotlib pandas seaborn

我有一个包含50个功能的5000个产品的数据集.其中一列是"颜色",列中有100多种颜色.我正在尝试绘制一个条形图,仅显示前10种颜色以及每种颜色中有多少种产品.

top_colors = df.colors.value_counts()
top_colors[:10].plot(kind='barh')
plt.xlabel('No. of Products');
Run Code Online (Sandbox Code Playgroud)

熊猫情节

使用Seaborn:

sns.factorplot("colors", data=df , palette="PuBu_d");
Run Code Online (Sandbox Code Playgroud)

Seaborn

1)有更好的方法吗?

2)我怎样才能用Seaborn复制这个?

3) How do i plot such that the highest count is at the top (i.e black at the very top of the bar chart)

mwa*_*kom 11

一个简单的技巧可能是反转绘图的y轴,而不是使用数据进行预测:

s = pd.Series(np.random.choice(list(string.uppercase), 1000))
counts = s.value_counts()
ax = counts.iloc[:10].plot(kind="barh")
ax.invert_yaxis()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Seaborn barplot当前不支持水平方向的条形图,但是如果要控制条形图的显示顺序,则可以将值列表传递给x_order参数.但无论如何,我认为在这里使用大熊猫绘图方法更容易.