在 python 2.7 中更改 matplotlib.bar 顺序

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

问题描述

在这个例子中:

import matplotlib.pyplot as plt颜色= ['白色','橙色','绿色','紫色']费率 = ['5','5','4','3']plt.bar(颜色,费率)

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

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

解决方案

Matplotlib 2.1 为

In this example:

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 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.

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 顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)