Python numpy数组从多个值创建条形图
我在一个奇怪的球的情况下,我有一个numpy数组,如下所示:Python numpy数组从多个值创建条形图,python,arrays,numpy,Python,Arrays,Numpy,我在一个奇怪的球的情况下,我有一个numpy数组,如下所示: >>> scores array([ (18.0, 'bill'), (23.0, 'sarah'), (12.0, 'stacy'), (71.0, 'joe'), (54.0, 'adam'), (87.0, 'kat'), (46.0, 'le'), (87.0, 'dave'), (89.0, 'kara')]) >>> scores = sort
>>> scores
array([
(18.0, 'bill'), (23.0, 'sarah'), (12.0, 'stacy'),
(71.0, 'joe'), (54.0, 'adam'), (87.0, 'kat'),
(46.0, 'le'), (87.0, 'dave'), (89.0, 'kara')])
>>> scores = sorted(
[(name, float(val)) for val, name in scores],
key=lambda x:x[1],
reverse=True
)
>>> plt.bar(*zip(*scores))
我试图根据y轴上的touple的[0]值得分和x轴上的touple的[1]值名称,使用上述数组创建条形图。能够将分数从最高点到最低点进行排序将是一个很好的加分……我被卡住了,真的不知道如何继续下去。非常感谢您的帮助/指导
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
x=[]
y=[]
for i in range(len(array)):
x.append(array[i][0])
y.append(array[i][1])
plt.bar(y,x, align='center', alpha=0.5)
plt.ylabel('name')
plt.xlabel('number')
plt.title('name/number')
plt.show()
输出:
您可以执行以下操作:
>>> scores
array([
(18.0, 'bill'), (23.0, 'sarah'), (12.0, 'stacy'),
(71.0, 'joe'), (54.0, 'adam'), (87.0, 'kat'),
(46.0, 'le'), (87.0, 'dave'), (89.0, 'kara')])
>>> scores = sorted(
[(name, float(val)) for val, name in scores],
key=lambda x:x[1],
reverse=True
)
>>> plt.bar(*zip(*scores))