用pandas/matplotlib或seaborn排序的条形图 [英] Sorted bar charts with pandas/matplotlib or seaborn

查看:135
本文介绍了用pandas/matplotlib或seaborn排序的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dataset of 5000 products with 50 features. One of the column is 'colors' and there are more than 100 colors in the column. I'm trying to plot a bar chart to show only the top 10 colors and how many products there are in each color.

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

使用Seaborn:

sns.factorplot("colors", data=df , palette="PuBu_d");

1)有更好的方法吗?

1) Is there a better way to do this?

2)我该如何与Seaborn复制此内容?

2) How can i replicate this with Seaborn?

3)如何绘制以使最高计数在顶部(即条形图的最顶部为黑色)

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)

推荐答案

一个简单的技巧可能是反转绘图的y轴,而不是处理数据:

An easy trick might be to invert the y axis of your plot, rather than futzing with the data:

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

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

Seaborn barplot doesn't currently support horizontally oriented bars, but if you want to control the order the bars appear in you can pass a list of values to the x_order param. But I think it's easier to use the pandas plotting methods here, anyway.

这篇关于用pandas/matplotlib或seaborn排序的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆