X轴中的字符串值的堆积直方图失败

时间:2016-12-04 14:22:20

标签: python python-2.7 pandas matplotlib

我有以下堆叠直方图的代码,当FIELD是数字时,它可以正常工作。但是,当我将FIELD_str而不是1,2,3,...包含abc1abc2abc3等时,它会因错误而失败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])

0 个答案:

没有答案