如何基于y值按降序绘制两个列表? [英] How to plot two lists in descending order based on y values?
本文介绍了如何基于y值按降序绘制两个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表.第一个是字符串列表 a
I have two lists. The first is a list of strings a
['Agriculture/Forestry/Fisheries/Veterinary Medicine',
'Architectural and Town Planning',
'Business Administration and Related', ...]
,第二个是浮点列表 b
[66667.0,
22283.0,
670091.5, ...]
当我使用以下代码绘制时
When I use the following code to plot it
import matplotlib.pyplot as plt
%matplotlib inline
a = ['Agriculture/Forestry/Fisheries/Veterinary Medicine',
'Architectural and Town Planning',
'Business Administration and Related', ...]
b = [66667.0,
22283.0,
670091.5, ...]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(a,b)
plt.xticks(rotation=90)
条根据列表 a
中的字符串按字母顺序排列.我将如何绘制它,以便相对于列表 b
以降序排列条形?
the bars are arranged in alphabetical order according to the strings in list a
.
How would I plot it so that the bars are arranged in descending order with respect to list b
?
推荐答案
一个简单的数据重排方法:
A simple data rearrangement approach:
import matplotlib.pyplot as plt
a = [
'Agriculture/Forestry/Fisheries/Veterinary Medicine',
'Architectural and Town Planning',
'Business Administration and Related'
]
b = [66667.0, 22283.0, 670091.5]
b, a = zip(*sorted(zip(b, a), reverse=True)) # reverse sort data on 'b'
c = range(len(b))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(c, b)
plt.xticks(c, a, rotation=90)
plt.show()
这篇关于如何基于y值按降序绘制两个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文