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!

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.