Change matplotlib.bar Order in python 2.7(在 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 2.7 中更改 matplotlib.bar 顺序


基础教程推荐
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01