简体   繁体   English

使用正确的标签将值从 x 轴切换到 y 轴(Python Matplotlib.pyplot 或 Seaborn)

[英]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.我希望 x 轴使用部门名称,y 轴使用总计列中的数字。 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.当我尝试对其进行编码时,x 轴是总数,y 轴是这些总数中有多少适合分箱的计数。

Title: deptgroups (my dataframe)标题:deptgroups(我的数据框)

department count数数
Admin行政 857 857
Engineering工程 26 26
IT 49 49
Marketing营销 16 16
Operations操作 1013 1013
Sales销售量 1551 1551

Data as "datagroups.csv"数据为“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.我试过指定 x 和 y 值,但它会引发错误。 I would like it to look more like this (ish):我希望它看起来更像这样(ish):

Qty数量 Dept 1 1部 Dept 2二部
500 500 XXXXXX XXXXXX ...... ……
400 400 XXXXXX XXXXXX ...... ……
300 300 XXXXXX XXXXXX XXXXXX XXXXXX
200 200 XXXXXX XXXXXX XXXXXX XXXXXX
100 100 XXXXXX XXXXXX XXXXXX XXXXXX

The original data for example would look like this: |Department |例如,原始数据如下所示:|部门| Count|计数| |-----------|------| |------------|------| |Dept 1 | |1部| 500 | 500 | |Dept 2 | |二部| 300 | 300 |

I think main confusion is coming from the name of the plot. It's called bar chart, histogram is something else.我认为主要的混淆来自 plot 的名称。它被称为条形图,直方图是另一回事。

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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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