简体   繁体   中英

Matplotlib bar graph organization

I have a CSV file on employee salaries in 2020 and I cant figure out how to organize my bar graph. here is the CSV for reference: https://catalog.data.gov/dataset/employee-salaries-2020

I would like to present the average salary of each department in a bar graph.

I've started by organizing the bar graph by Department and its value_count() but I would like the x axis to represent the average salary in that department. Any tips on how I can achieve this?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

file_path = 'Employee_Salaries_-_2020.csv'
salaries = pd.read_csv(file_path)

a = salaries.Department.value_counts()
x = list(a.index)
y = list(a)

f, ax = plt.subplots(figsize=(20,10))
width = 0.75 # the width of the bars 
ind = np.arange(len(y))  # the x locations for the groups
ax.barh(ind, y, width, color="blue")
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
for i, v in enumerate(y):
    ax.text(v + .25, i + .25, str(v), color='blue', fontweight='bold') #add value labels into bar
plt.title('Average Base Pay by Department')
plt.xlabel('Average Base Pay')
plt.ylabel('Department')
plt.show()

按部门及其 value_count() 划分的图表

Instead of value counts you can get the average salary by doing salaries.groupby('Department')['Base Salary'].mean() . This should be the value you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM