How can I label each boxplot in a seaborn plot with the median value?
E.g.
import seaborn as sns sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips)
How do I label each boxplot with the median or average value?
How to Show mean marks on Boxplot with Seaborn? With Seaborn's boxplot() function, we can add a mark for mean values on the boxplot, using the argument “showmeans=True”. Seaborn's showmeans=True argument adds a mark for mean values in each box. By default, mean values are marked in green color triangles.
orient“v” | “h”, optional. Orientation of the plot (vertical or horizontal). This is usually inferred based on the type of the input variables, but it can be used to resolve ambiguity when both x and y are numeric or when plotting wide-form data. colormatplotlib color, optional.
I love when people include sample datasets!
import seaborn as sns sns.set_style("whitegrid") tips = sns.load_dataset("tips") box_plot = sns.boxplot(x="day",y="total_bill",data=tips) medians = tips.groupby(['day'])['total_bill'].median() vertical_offset = tips['total_bill'].median() * 0.05 # offset from median for display for xtick in box_plot.get_xticks(): box_plot.text(xtick,medians[xtick] + vertical_offset,medians[xtick], horizontalalignment='center',size='x-small',color='w',weight='semibold')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With