在条形图例熊猫中为整数分配标签

时间:2019-12-20 14:18:19

标签: pandas bar-chart

我想将'Single'分配给0,将'Married'分配给1,并在条形图中显示  enter image description here

这是我写的代码:

ds["Marital_Status"].value_counts().plot(kind='bar',color=['green', 'cyan'], rot=0)

plt.title(“购买者的婚姻状况”)

2 个答案:

答案 0 :(得分:1)

使用Series.map

(  ds["Marital_Status"].map({0:'Single',1:'Married'})
                       .value_counts()
                       .plot(kind='bar',color=['green', 'cyan'], rot=0) )
   # if is string type
   #ds["Marital_Status"].map({'0':'Single','1':'Married'}) 

答案 1 :(得分:0)

在这种情况下,只需重命名刻度即可:

ax = ds["Marital_Status"].value_counts().plot(kind='bar',color=['green', 'cyan'], rot=0)

ax.set_xticklabels(['Single', 'Married'])
相关问题