[英]How to create a grouped bar chart (by month and year) on the x-axis and values on the y-axis using matplotlib.pyplot?
[英]Switch the values from x-axis to y-axis while using the correct labels(Python Matplotlib.pyplot OR Seaborn)
我有一个小的信息表,我正试图将其转换为直方图。 它有一列部门名称和第二列总计。 我希望 x 轴使用部门名称,y 轴使用总计列中的数字。 当我尝试对其进行编码时,x 轴是总数,y 轴是这些总数中有多少适合分箱的计数。
标题:deptgroups(我的数据框)
部 | 数数 |
---|---|
行政 | 857 |
工程 | 26 |
它 | 49 |
营销 | 16 |
操作 | 1013 |
销售量 | 1551 |
数据为“datagroups.csv”
department,count,
Admin,857,
Engineering,26,
IT,49,
Marketing,16,
Operations,1013,
Sales,1551
plt.hist(x=deptgroups)
plt.show()
我试过指定 x 和 y 值,但它会引发错误。 我希望它看起来更像这样(ish):
数量 | 1部 | 二部 |
---|---|---|
500 | XXXXXX | …… |
400 | XXXXXX | …… |
300 | XXXXXX | XXXXXX |
200 | XXXXXX | XXXXXX |
100 | XXXXXX | XXXXXX |
例如,原始数据如下所示:|部门| 计数| |------------|------| |1部| 500 | |二部| 300 |
我认为主要的混淆来自 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.