在python 2.7中更改matplotlib.bar的顺序 [英] Change matplotlib.bar Order in python 2.7

查看:320
本文介绍了在python 2.7中更改matplotlib.bar的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中:

import matplotlib.pyplot as plt

colors = ['white', 'orange', 'green', 'purple']
rates = ['5','5','4','3']

plt.bar(colors, rates)

图中的条形图按字母顺序排列.

the bars in the plot are ordered by the alphabetic order.

如何使它们以与代码中相同的顺序显示(从最高到最低)?

How do I make them to show in the same order as they are in the code (from highest to lowest rate)?

推荐答案

Matplotlib 2.1为已删除.

Matplotlib 2.1 provides the new option to use strings as input to its plotting functions - called "categoricals". This is a new feature and not yet completely working. One caveat is that strings are automatically sorted (alphabetically). This restriction will be removed in the next release.

在此之前,您将生成图表,就像在matplotlib甚至不允许输入字符串的时候一样.将数据转换为数字,并根据列表索引进行绘制.然后将ticklabel设置为其名称.

Until then you would produce your plot just as in times when matplotlib did not even allow for strings to put in. Convert data to numbers, and plot against the list index. Afterwards set the ticklabels to their names.

import matplotlib.pyplot as plt

colors = ['white', 'orange', 'green', 'purple']
rates = ['5','5','4','3']

plt.bar(range(len(colors)), list(map(float,rates)) )
plt.xticks(range(len(colors)), colors)

plt.show()

这篇关于在python 2.7中更改matplotlib.bar的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆