Issue
# 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
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.