Home > Back-end >  Seaborn violinplots: how to I obtain line paths for violin edges?
Seaborn violinplots: how to I obtain line paths for violin edges?

Time:12-16

I am using Seaborn to plot two violin plots, and I would like to obtain the X-coordinates of the edges of the violins, such that I may subtract one from the other to find the difference between the two KDEs/distributions. I suspect it has to do with the properties of the matplotlib.collections.PolyCollection objects, but I have had difficulty navigating the documentation for this - so I apologize I donnot have much code to attach... but I'm including my current violin plot in case that helps

import pandas as pd
import seaborn as sns

observed_results = [11.10283128625, 6.031906445000001, 4.625099850384, 4.371541683749999, 4.188776438315, 4.169933187839999, 4.147271982216, 3.137545605726, 2.727390606468, 2.706991071933, 2.483074510875, 2.470624399684, 2.45608460474, 2.413902898276, 2.390530982763, 2.347653087613, 2.3049660823, 2.173520711313, 2.114398085409, 2.072213409552, 1.96126510972, 1.768724290017, 1.722913211104, 1.715972042575, 1.71293343376, 1.686909025847, 1.546933962564, 1.520621928225, 1.50428319008, 1.4944074417, 1.409957657136, 1.40292975245, 1.3157577856, 1.3078804375, 1.2974806016, 1.288403682732, 1.236493409437, 1.225900768752, 1.222094652926, 1.202655483344, 1.109818003441, 1.108678687017, 1.103788138352, 1.066656041116, 0.9799193812, 0.9729697610879998, 0.9532061536159999, 0.908066599737, 0.8847958337999999, 0.8698585519490001, 0.859714675264, 0.8422146736200001, 0.8229930509580001, 0.79569571686, 0.79170962662, 0.7855221024799999, 0.775488805524, 0.7690100510069999, 0.765153773568, 0.677797640336, 0.62878587992, 0.6278724034559998, 0.619961861949, 0.555740507912, 0.5458990340579999, 0.495263271752, 0.4744517001510001, 0.453783299787, 0.426952628919, 0.4079525625, 0.4030645275, 0.401266962432, 0.3807181439999999, 0.3313936548, 0.2707379496719999, 0.256952683998, 0.248430471184, 0.242090703552, 0.235644786034, 0.214602373804, 0.199521746592, 0.1951300125, 0.173116351962, 0.172562334309, 0.156660445172, 0.145336979139, 0.1190036898, 0.114659525376, 0.094888354288, 0.06696725615, 0.0305541639, 0.023464003706, 0.021922131125]
observed_labels = ["Observed" for n in observed_results]

expected_results = [4.5217885770490405, 3.828641396489095, 3.4231762883809305, 3.1354942159291497, 2.91235066461494, 2.7300291078209855, 2.575878427993727, 2.4423470353692043, 2.324563999712821, 2.2192034840549946, 2.12389330425067, 2.03688192726104, 1.9568392195875037, 1.8827312474337816, 1.8137383759468302, 1.749199854809259, 1.6885752329928243, 1.6314168191528757, 1.5773495978825998, 1.5260563034950494, 1.4772661393256175, 1.4307461236907244, 1.3862943611198906, 1.3437347467010947, 1.3029127521808397, 1.2636920390275583, 1.2259517110447113, 1.1895840668738362, 1.1544927470625663, 1.120591195386885, 1.087801372563894, 1.0560526742493137, 1.02528101558256, 0.995428052432879, 0.9664405155596267, 0.9382696385929304, 0.9108706644048158, 0.8842024173226546, 0.858226930919394, 0.832909122935104, 0.8082165103447325, 0.7841189587656721, 0.760588461355478, 0.7375989431307791, 0.7151260872787205, 0.6931471805599453, 0.6716409753389818, 0.6505875661411494, 0.6299682789384137, 0.6097655716208943, 0.5899629443247145, 0.570544858467613, 0.5514966634969185, 0.5328045304847661, 0.5144553918165694, 0.496436886313891, 0.4787373092144902, 0.46134556650262093, 0.44425113314332093, 0.4274440148269396, 0.4109147128757291, 0.39465419200394874, 0.37865385065750773, 0.3629054936893685, 0.34740130715340317, 0.3321338350226148, 0.31709595765807425, 0.3022808718729337, 0.2876820724517809, 0.2732933349996814, 0.2591087000077249, 0.2451224580329851, 0.2313291359006492, 0.21772348384487053, 0.20430046351272993, 0.19105523676270922, 0.17798315519535654, 0.16507975035944858, 0.1523407245820189, 0.13976194237515874, 0.1273394223766015, 0.11506932978478723, 0.10294796925244237, 0.09097177820572676, 0.07913732055872386, 0.06744128079553265, 0.055880458394456614, 0.04445176257083381, 0.03315220731690051, 0.02197890671877523, 0.010929070532190317, -0.0]
expected_labels = ["Expected" for n in expected_results]

all_results = observed_results   expected_results
all_labels = observed_labels   expected_labels
df_longform = {r"Z-score": all_results, 'Condition': all_labels}
df_longform = pd.DataFrame(data=df_longform)

ax = sns.violinplot(x='Condition', y=r"Z-score", data=df_longform, inner=None,
                        scale='area', bw=0.3, width=0.8, saturation=1,
                        linewidth=1, cut=0)
print(ax.collections)
plt.show()

Please let me know if there if anything about this question is unclear, or there is anything else I forgot to provide here. Can't figure this out for the life of me, so any advice is really appreciated -

CodePudding user response:

Here is a comparison between:

  • comparing kdeplot, violinplot and boxenplot

  • Related