0

I am in a odd ball situation where I have a numpy array like the following:

>>> 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')])

I am trying to create a bar graph with the above array based on the touple's [0] value (score) in y axis, and touple's [1] value (name) in the x axis. Being able to sort the score is from highest to lowest would be a great plus....I am stuck and really don't know how to proceed with this. Any help/guidance is highly appreciated!

olive
  • 171
  • 1
  • 2
  • 12

3 Answers3

3

You can do the following:

>>> scores = sorted(
                     [(name, float(val)) for val, name in scores], 
                     key=lambda x:x[1], 
                     reverse=True
                    )
>>> plt.bar(*zip(*scores))

enter image description here

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

scores = np.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_df = pd.DataFrame(scores, columns=["score", "name"])
scores_df.score = scores_df["score"].astype(float)
scores_df.sort_values("score", ascending=False, inplace=True)
scores_df.plot.bar(x="name", y="score")

Output: enter image description here

bigbounty
  • 16,526
  • 5
  • 37
  • 65
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()

enter image description here

Zesty Dragon
  • 551
  • 3
  • 18