如何在pyplot图表中对条形进行排序?

时间:2019-09-07 12:26:51

标签: python matplotlib charts

我有一个熊猫系列,电影类型作为索引,人气得分作为值。我想创建一个条形图,以显示每种流派的流行度得分,并根据其得分从高到低对流派进行排序。

当我尝试这样做时,条形总是按字母顺序排序。我已经找到了与此问题相关的主题,但是,建议的解决方案似乎始终是在创建图表之前对系列中的值进行预排序。在我的系列文章中,值已经按降序排列,但是条形图仍然按字母顺序对条形进行排序。

我尝试按降序对系列中的值进行排序。我也很讨厌将值用作索引

系列top5_1960

genres
Animation    1.011207
Adventure    0.740373
Family       0.619815
Thriller     0.603597
TV Movie     0.564817

创建条形图的代码

def bar_plot(x,y,title,xlabel,ylabel):
    plt.figure(figsize=(8,5))
    plt.bar(x,y)
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.show()

使用参数调用函数

heights_1960 = (top5_1960.values)
labels_1960 = (top5_1960.index)
ylabel = 'Avg.popularity score'
xlabel = 'Genres'
title = 'Top 5 favorite genres of 1960s'

bar_plot(labels_1960, heights_1960, title, xlabel, ylabel)

0 个答案:

没有答案