Consider a simple DistributionChart such as the following:

data = Table[RandomVariate[NormalDistribution[RandomInteger[5], 1], 100], {3}];

enter image description here

I want to center those three violins at desired x values on an actual x-axis (not just label them using ChartLabels), for example at x = {1.1, 2.5, 4.0}. The resulting horizontal spacing between the violins will depend on the x list and I should be able to Show the resulting plot overlaid with a regular Plot, ListPlot, etc. How can I do this?


1 Answer 1

data = Table[
   RandomVariate[NormalDistribution[RandomInteger[5], 1], 100], {3}];

A bit hacky, but these polygons are generated and translated to positions {1.,0.},{2.,0.} and {3.,0.} respectively. This can be changed by finding the positions where these translations are happening.

Starting with chart p2, locate where the GeometricTransformation is happening and append the {2,2} which is the x translation component:

p2 = DistributionChart[data]

pos = Join[#, {2, 2}] & /@ Position[p2, GeometricTransformation[_, _]]

xpos = {1.1, 2.5, 4.0};
xtx = {#, 0} & /@ xpos;

Change the translation:

Table[Part[p2, Sequence @@ pos[[i]]] = xtx[[i]], {i, Length@pos}]

Now, superimpose on p1 which is a sin curve for reference.

p1 = Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotRange -> All];

Show[p1, p2]

enter image description here

  • 1
    $\begingroup$ This works nicely. Thank you! $\endgroup$
    – FNia
    Sep 29, 2022 at 7:14
  • $\begingroup$ You are welcome. Thanks for the accept. $\endgroup$
    – Syed
    Sep 29, 2022 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.