Try to make a Y-axis in order Python

Question:

I try to make a bar graph but I run into the problem that my y-axis is not in an order.

[enter image description here][1]
[1]: https://i.stack.imgur.com/6L8LW.png

import numpy as np
import matplotlib.pyplot as plt

# data to plot
numbers = 5
male_salary = ['70942842', '178332', '9570972', '409174', '1494387']
female_salary = ['32873995', '1494387', '7380776', '198272', '697929']

# create plot
fig, ax = plt.subplots()
index = np.arange(numbers)
width = 0.40
opacity = 0.75
rects1 = plt.bar(index, male_salary, width, alpha=opacity, color='b',label='Male')
rects2 = plt.bar(index + width, female_salary, width, alpha=opacity, color='g', label='Female')
plt.xlabel("Race")
plt.ylabel("The total salary")
plt.title("Sex Differences in Salary were presented in the Private work class")
plt.xticks(index + width, ("W", "AIE", "B", "O", "API"))    

plt.legend()
plt.show()
Asked By: Chang Dang

||

Answers:

The mal_salary and female_salary arrays are considered as text/string if you encase it in quotes. Remove the quotes and try…

import numpy as np
import matplotlib.pyplot as plt

# data to plot
numbers = 5
male_salary = [70942842, 178332, 9570972, 409174, 1494387]
female_salary = [32873995, 1494387, 7380776, 198272, 697929]

# create plot
fig, ax = plt.subplots(figsize=(10,5))
index = np.arange(numbers)
width = 0.40
opacity = 0.75
rects1 = plt.bar(index, male_salary, width, alpha=opacity, color='b',label='Male')
rects2 = plt.bar(index + width, female_salary, width, alpha=opacity, color='g', label='Female')
plt.xlabel("Race")
plt.ylabel("The total salary")
plt.title("Sex Differences in Salary were presented in the Private work class")
plt.xticks(index + width, ("W", "AIE", "B", "O", "API"))    

plt.legend()
plt.show()

enter image description here

Answered By: Redox
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.