Search code examples
pythonpython-3.xmatplotlibhistogramaxis

Switch the values from x-axis to y-axis while using the correct labels(Python Matplotlib.pyplot OR Seaborn)


I have a small table of information that I'm trying to turn into a histogram. It has one column of Department names and a second column of totals. I would like the x-axis to use the Department names and the y-axis to use the numbers from the totals column. When I try to code it, the x-axis is the totals and the y-axis is a count of how many of those totals fit into the bins.

Title: deptgroups (my dataframe)

department count
Admin 857
Engineering 26
IT 49
Marketing 16
Operations 1013
Sales 1551

Data as "datagroups.csv"

department,count,
Admin,857,
Engineering,26,
IT,49,
Marketing,16,
Operations,1013,
Sales,1551
plt.hist(x=deptgroups)
plt.show()

Incorrect Graph

I've tried specifying x and y values, but it throws an error. I would like it to look more like this (ish):

Qty Dept 1 Dept 2
500 XXXXXX ......
400 XXXXXX ......
300 XXXXXX XXXXXX
200 XXXXXX XXXXXX
100 XXXXXX XXXXXX

The original data for example would look like this: |Department | Count| |-----------|------| |Dept 1 | 500 | |Dept 2 | 300 |


Solution

  • I think main confusion is coming from the name of the plot. It's called bar chart, histogram is something else.

    import matplotlib.pyplot as plt
    
    data = {
        'Admin': 857,
        'Engineering': 26,
        'IT': 49,
        'Marketing': 16,
        'Operations': 1013,
        'Sales': 1551
    }
    departments = data.keys()
    count = data.values()
    
    # you don't need histogram, you need bar plot
    plt.bar(departments, count, color='blue', width=0.4)
    plt.xlabel('Department')
    plt.ylabel('Count')
    plt.show()
    

    enter image description here