使用 pandas/matplotlib 或 seaborn 对条形图进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28022227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:51:25  来源:igfitidea点击:

Sorted bar charts with pandas/matplotlib or seaborn

pythonmatplotlibpandasseaborn

提问by ananuc

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.

我有一个包含 5000 个具有 50 个特征的产品的数据集。其中一列是“颜色”,该列中有 100 多种颜色。我试图绘制一个条形图以仅显示前 10 种颜色以及每种颜色有多少产品。

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

Pandas Plot

Pandas图

Using Seaborn:

使用 Seaborn:

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

Seaborn

海伯恩

1) Is there a better way to do this?

1)有没有更好的方法来做到这一点?

2) How can i replicate this with Seaborn?

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)

3)我如何绘制使得最高计数位于顶部(即条形图最顶部的黑色)

回答by mwaskom

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

一个简单的技巧可能是反转绘图的 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()

enter image description here

在此处输入图片说明

Seaborn barplotdoesn'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_orderparam. But I think it's easier to use the pandas plotting methods here, anyway.

Seabornbarplot目前不支持水平方向的条,但如果您想控制条出现的顺序,您可以将值列表传递给x_order参数。但我认为无论如何在这里使用Pandas绘图方法更容易。

回答by elyase

If you want to use pandas then you can first sort:

如果你想使用Pandas,那么你可以先排序:

top_colors[:10].sort(ascending=0).plot(kind='barh')

Seaborn already styles your pandas plots, but you can also use:

Seaborn 已经为您的Pandas图设置了样式,但您也可以使用:

sns.barplot(top_colors.index, top_colors.values)