【发布时间】:2016-12-04 12:42:51
【问题描述】:
我正在以这种方式创建直方图:
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()
数据:
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
【问题讨论】:
-
@cel:谢谢。事实上,我已经检查了本教程并尝试将其应用于我的案例,但它给了我一条错误消息(请参阅我的编辑)。
-
能否请您也提供您的数据?
-
@DimKoim:好的,请看我最后的编辑。
-
@cel:在我看来,这个解决方案对于我的情况来说过于复杂了。我找到了另一个(更简单)的解决方案(见我的回答)。
标签: python python-2.7 matplotlib