更改python 2.7中的matplotlib.bar顺序[重复]
这个问题已经在这里有了答案 :
Pyplot自动对y值排序 (2个答案)
3年前关闭。
在此示例中:
import matplotlib.pyplot as plt
colors = ['white', 'orange', 'green', 'purple']
rates = ['5','5','4','3']
plt.bar(colors, rates)
图中的条按字母顺序排序。
如何使它们以与代码中相同的顺序显示(从最高到最低)?
-
Matplotlib
2.1提供了新选项,可将字符串用作其绘图功能的输入-称为“类别”。这是一项新功能,尚未完全正常运行。一个警告是字符串会自动(按字母顺序)排序。此限制将在下一版本中删除。在此之前,您将生成图表,就像在matplotlib甚至不允许输入字符串的时候一样。将数据转换为数字,并根据列表索引进行绘制。然后,将ticklabel设置为其名称。
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()