我正在以这种方式创建直方图:
selected_features = ["HOUR","CLUSTER"]
plt.figure(1)
plt.hist(df[selected_features].values[:,0])
plt.title("HOUR")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.draw()
plt.show()
CLUSTER
的值为0或1。
现在我想将每个条形图转换为带有2个区域的堆叠条形图(让我们说红色和绿色),以显示每个条形的CLUSTER
值的分布。我该怎么办?
编辑:
我试过这个,但它给了我错误ValueError: incompatible sizes: argument 'height' must be length 9 or scalar
:
N=9
ind = np.arange(N) # the x locations for the groups
width = 0.35
p1 = plt.bar(ind, df[df["CLUSTER"]==0][['HOUR']],color='r')
p2 = plt.bar(ind, df[df["CLUSTER"]==1][['HOUR']],color='y')
plt.legend((p1[0], p2[0]), ('0', '1'))
plt.show()
DATA:
s_hour = pd.Series(["5","5","5","8","8","9","10"])
s_cluster = pd.Series(["1","1","0","1","0","1","0"])
df = pd.concat([s_hour, s_cluster], axis=1)
df
答案 0 :(得分:0)
我是这样做的:
filter = df["CLUSTER"] == 1
plt.hist([df["HOUR"][filter],df["HOUR"][~filter]],stacked=True)
plt.show()