[英]matplotlib - how to plot bar graphs side-by-side to compare values between 2 columns
我有一個由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')
使用分組。 就像是:
for group in df.groupby('year'):
group.plot(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.