我有以下堆叠直方图的代码,当FIELD
是数字时,它可以正常工作。但是,当我将FIELD_str
而不是1,2,3,...包含abc1
,abc2
,abc3
等时,它会因错误而失败TypeError: cannot concatenate 'str' and 'float' objects
。如何(直接或间接)替换X轴上的数字及其字符串值(这是图表更好的可读性所必需的):
filter = df["CLUSTER"] == 1
plt.ylabel("Absolute frequency")
plt.hist([df["FIELD"][filter],df["FIELD"][~filter]],stacked=True,
color=['#8A2BE2', '#EE3B3B'], label=['1','0'])
plt.legend()
plt.show()
数据集:
s_field1 = pd.Series(["5","5","5","8","8","9","10"])
s_field1_str = pd.Series(["abc1","abc1","abc1","abc2","abc2","abc3","abc4"])
s_cluster = pd.Series(["1","1","0","1","0","1","0"])
df = pd.concat([s_field1, s_field1_str, s_cluster], axis=1)
df
编辑:
我试图创建一个字典,但无法弄清楚如何将它放在直方图中:
# since python 2.7
import collections
yes = collections.Counter(df["FIELD_str"][filter])
no = collections.Counter(df["FIELD_str"][~filter])