[英]Barplot of numerical categorical data of number of observations ordered numerically in matplotlib
I have numerical categorical data in a dataframe which I want to bar plot and order in ascending order by value.我在 dataframe 中有数字分类数据,我想禁止 plot 并按值升序排列。 I am able to do it in seaborn easily, but cannot figure out how to order it in pandas or matplotlib.
我可以在 seaborn 轻松做到这一点,但无法弄清楚如何在 pandas 或 matplotlib 中订购它。 Here is the code, as I said, the seaborn plot is ordered correct, but the pandas plot is not.
这是代码,正如我所说,seaborn plot 的顺序是正确的,但 pandas Z322FA86E1B78A944D 不是。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({
'category':['blue','green','red','blue','green','red','blue','green','red','violet'],
'income':[15,1,4,5,3,2.5,3.4,4.4,12,1.6],
})
print(df)
df['income_cat'] = np.ceil(df["income"]/1.5)
df["income_cat"].where(df["income_cat"]<5,5.0,inplace=True)
df['income_cat'].value_counts().plot(kind='bar',sort_columns=True)
sns.catplot(x='income_cat',kind='count',data=df)
plt.show()
Also, any other suggestions to make the plot more "meanigful" are welcome.此外,欢迎任何其他使 plot 更“有意义”的建议。
I assume you want to order the bars by the value of the index.我假设您想按索引值对条形图进行排序。 That can be accomplished by using
sort_index()
:这可以通过使用
sort_index()
来完成:
df['income_cat'].value_counts().sort_index().plot(kind='bar',sort_columns=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.