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 顺序


- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01