How to get bar y axis in ascending order starting from zero

Question:

# Set x_axis, y_axis & Tick Locations
x_axis = final_df["title"]
ticks = np.arange(len(x_axis))
y_axis = final_df["salary"]
plt.bar(x_axis, y_axis, align="center", alpha=0.5, color=["k", "r", "g", "m", "b", "c", "y"])
plt.xticks(ticks, x_axis, rotation="vertical")
plt.ylabel("Salaries ($)")
plt.xlabel("Employee Titles")
plt.title("Average Employee Salary by Title")
#plt.savefig("Average_salary_by_title.png")
plt.show()

My graph looks like pic Bar1 and I want pic Bar2. I’ve tried different formatting for the y axis, but the end result is not similar to Bar2

pic Bar1
pic Bar2

Asked By: David H

||

Answers:

It seems like the values in the "salary" column are strings.
In this case add the following to your code:

replace_pattern = r'$|,'  

final_df['salary'].replace(replacement_pattern, '', regex=True, inplace=True)  # replace $ and , 
                                                                               # with an empty string, 
                                                                               # needed for float conversion

final_df['salary'] = final_df['salary'].astype(float)

# continue with barplot

This will convert the entries in the "salary" column to floats.

Answered By: Phil Leh

If you have a list with strings and want to convert it into floats

new_list_floats = [float(x) for x in list_name]
Answered By: Vaibhav Yalla
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.