0

I have plotted a stacked bar chart using sns.histplot() and I wish to add percentage (the values in the column 'Cancellation Rate') on each bar with corresponding areas, but how can I do that? I have searched a lot of similar questions but still couldn't find a solution... Most of them are using plt directly, few of them are using seaboan:( Thanks for anyone who can help me!

The following is my dataset and my code:

data = np.array([['Corporate', 0, 84.7986141186661],
       ['Corporate', 1, 15.201385881333913],
       ['Direct', 0, 86.51926915399969],
       ['Direct', 1, 13.480730846000307],
       ['Groups', 0, 57.60795065113091],
       ['Groups', 1, 42.39204934886909],
       ['Offline TA/TO', 0, 84.76980728051392],
       ['Offline TA/TO', 1, 15.230192719486082],
       ['Online TA', 0, 64.75830560099273],
       ['Online TA', 1, 35.24169439900728]], dtype=object)
resort = pd.DataFrame(data = data, columns = ['Market Segment','is_canceled','Cancellation Rate'])
fig, ax1 = plt.subplots(figsize=(8,4))
sns.histplot(x='Market Segment',
            weights='Cancellation Rate',
            multiple='stack',
             shrink=0.8,
            hue='is_canceled',
            data=resort,
            palette=sns.color_palette("Set1", 2),
            ax = ax1
            ).set(title = 'Cancellation Rate of Booking Market Segment')

The plot I got is:enter image description here

The plot I expect to have is like the following,where percentages appear in each hue segment on each bar: enter image description here

0 Answers0