Skip to content
Advertisement

Connecting means in seaborn box plot

I want to connect box plot means. I can do the basic part but cannot connect box plot means and box plots offset from x axis. similar post but not connecting means Python: seaborn pointplot and boxplot in one plot but shifted on the x-axis

JavaScript

As we can see the sns.line plot does not follow the means and box plots and names in the x axis has offset.

How can I fix this ?

enter image description here

Advertisement

Answer

When dealing with seaborn plot, I would strongly recommend you always provide an order= (and hue_order= if applicable) to avoid nasty surprise with the categories not showing up in a consistent order between calls.

For the purpose of your question, you can replace the lineplot with a pointplot, which will automatically aggregate the values by categories and plot using a line

JavaScript

enter image description here

If for some reason you don’t want to or cannot use a seaborn function that takes an order argument, then aggregate by hand in pandas, and reindex() with your order to make sure the values appear in the right order in the dataframe before plotting with the tool of your choice.

For instance, you could replace the call to pointplot() above with:

JavaScript

and have the exact same result

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement