【发布时间】:2018-09-13 19:56:10
【问题描述】:
我正在使用条形图绘制一些分类数据。即使我对数据框值进行排序,Matplotlib 也会按字母顺序对我的 x 轴进行排序。 这是我的代码:
fig3, new_ax = plt.subplots(1,1, figsize=(25/3,5))
summary = tsa.source.sum().sort_values(ascending=False)
new_ax.bar(summary.index, summary.values, color=my_colors)
new_ax.legend()
bar_heights(new_ax) # custom function to get the values on top of bars
simpleaxis(new_ax) # custom function to define an axis to please my boss...
new_ax.set_ylabel('Effectifs')
new_ax.set_xlabel("Type d'arme")
new_ax.grid(False)
但这是摘要的样子,这就是我想在图表上看到的顺序:
famas 2214.0
aut_typebruit 759.0
grena 200.0
flg 78.0
douze 72.0
sept 53.0
dtype: float64
这是我的数据示例的链接: https://files.fm/u/wumscb4q 用这一行导入它:
tsa.source = pd.read_csv('sample.csv', sep=';', index_col=0)
这里是我的功能:
def simpleaxis(ax):
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
def bar_heights(axes):
for rect in axes.containers[0]:
height = rect.get_height()
rect.axes.text(rect.get_x() + rect.get_width()/2., height+3,
'%d' % int(height),
ha='center', va='bottom')
【问题讨论】:
-
我无法复制您的问题。想发minimal reproducible example?
-
完成了,我想...
-
不太最小。
plt.bar(['foo', 'bar'], [1, 2])呢?它是否按字母顺序对条形进行排序? -
是的...!
-
不知道为什么这被否决了 - 完全合理的问题和我也遇到过的问题。我将其投票恢复为中立。
标签: python python-3.x matplotlib