Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use an irregularly spaced, non-categorical axis on a categorical plot in seaborn

I'm creating a violin plot in Seaborn, which by default, assumes that the x-axis is categorical, and therefore evenly spaces the data, rather than scaling it by a value. I would like the spacing between the individual violins to be defined by values associated with each violin, rather than just spacing them evenly. I have read a number of things suggesting that I can overwrite the defaults with matplotlib commands, but can't get anything to work.

sns.set(palette='muted', color_codes=True)
f, axes = plt.subplots(2, 2, figsize=(8,5))
sns.violinplot(x = lsdf['6MO_CUM_MBO/1000FT'], y = lsdf.RELATIVE_DEPTH,
data=lsdf, palette="Blues", ax=axes[0,0])

I think the key issue here, is I'm not exactly sure what Seaborn's defaults are controling. Do I need to modify the axes object created by subplots? or the ax=[0,0] object?

The only answer I found to a similar question had a solution which was just, "do it in matplotlib," but I need the plots available in seaborn. Thanks for your help.

seaborn categorical violin plot

like image 665
Jonathan Fry Avatar asked Oct 30 '25 09:10

Jonathan Fry


1 Answers

Well, I eventually solved this, sort of... I caved and used pure matplotlib, gave up on Seaborn. The matplotlib violinplot takes an array-like positions argument which when passed numeric values, auto-scales the x-axis and behaves exactly as plt.plot or any plot where marker position and axis range are derived from the input data. I still used seaborn.set() to get the nice Seaborn aesthetic. There is matplotlib violin plot customization documentation which has nice examples of how to edit the details of the violins which allowed me to customize the violins and mimic all parts of the Seaborn violin plot.

like image 120
Jonathan Fry Avatar answered Nov 01 '25 23:11

Jonathan Fry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!