Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带pandas/matplotlib或seaborn的已排序条形图_Python_Matplotlib_Pandas_Seaborn - Fatal编程技术网

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

Python 带pandas/matplotlib或seaborn的已排序条形图,python,matplotlib,pandas,seaborn,Python,Matplotlib,Pandas,Seaborn,我有一个包含5000个产品和50个功能的数据集。其中一列是“颜色”,列中有100多种颜色。我试图绘制一个条形图,只显示前10种颜色以及每种颜色中有多少种产品 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");

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

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) 有更好的方法吗

2) 我如何用Seaborn复制这一点


3) 如何进行绘图,使最高计数位于顶部(即条形图顶部为黑色)

如果要使用熊猫,则可以首先排序:

top_colors[:10].sort(ascending=0).plot(kind='barh')
Seaborn已经为您的熊猫地块设置了样式,但您也可以使用:

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

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


Seaborn
barplot
目前不支持水平方向的条形图,但如果您想控制条形图的显示顺序,可以将值列表传递给
x_order
参数。但我认为这里使用熊猫绘图方法更容易。

谢谢。为了澄清,颜色是其中的一列。因此,您的答案将绘制整个数据集,而不仅仅是颜色列。尝试了df.colors[:10]。排序(升序=0)。绘图(kind='barh')无效。Seaborn也一样。有什么想法吗?有错误:“AttributeError:‘NoneType’对象没有属性‘plot’”。Seaborn在这种情况下似乎不起作用。谢谢,我解决了。top_colors.sort()top_colors[-10:].plot(kind='barh')示例来自
seaborn
docs: