python - matplotlib - 如何并排绘制条形图以比较两列之间的值
问题描述
我有一个由 2008 年和 2013 年分隔的某些值的数据集。但是,当我使用 matplotlib 绘制它们时,条形图中只有 2008 年的值出现。
我想将 2008 年和 2013 年的条形图并排比较。
import numpy as np
import matplotlib.pyplot as plt
data = np.genfromtxt('C:\data/IT8701_CA2_Data/hdb-resident-population-by-flat-type.csv',
skip_header=1,
dtype=[('shs_year','U50'),('flat_type','U50'),('resident_population','i8')], delimiter=",",
missing_values=['na','-'],filling_values=[0])`
labels = list(set(data['flat_type']))
labels.sort()
residents = np.arange(0,len(labels))
residents_values = data[['flat_type','resident_population']]
values = residents_values['resident_population']
units_values = {}
for i in labels:
valuesforFY = values[residents_values['flat_type']==i]
print("No.of Residents in Flat_type: " + i + " is {}".format(valuesforFY))
#the line below is critical
units_values[i] = valuesforFY
plt.figure(1, figsize=(8,8))
xdata = list(units_values.keys())
ydata = [i[0] for i in units_values.values()]
barchart = plt.bar(xdata, ydata, color='b')
解决方案
推荐阅读
- oracle - 不能将超过 2 行导出到 csv
- arrays - 带有 NodeJs 的 Restful API 和带有“返回”对象的 Mysql 情况
- c# - 通过网络发送文件是否比下载更快?
- vba - 连接 Amazon RDS for Oracle
- java - Citrus XPath 验证找不到元素
- hazelcast - 如何通过加入另一个节点来执行 Jet 作业?
- ruby-on-rails - 错误:运行 rake db:migrate 后,'NameError: undefined local variable or method 'rvm' for main:Object'
- javascript - 有没有办法确定“Enter”按键是否来自 Angular Material 自动完成?
- c++ - CString 赋值正在改变 GetLastError() 的值
- amazon-web-services - 当我们进行 AWS API Gateway 自定义授权 lambda 函数调用时,是否可以返回获取新的自定义标头属性?(连同政策)