I am looking to compare three different distributions by making a chart with the three violin plots via DistributionChart. The distributions have different sample sizes, and by default each distribution has a uniform maximum width. Is it possible to scale the maximum width of each distribution by the sample size of that distribution such that the three distributions on the single chart have different maximum widths?

  • $\begingroup$ Edit your question to provide a minimal working example including code and data. $\endgroup$
    – Bob Hanlon
    Jul 11, 2018 at 15:51
  • $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Please Edit to show your due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$
    – rhermans
    Jul 11, 2018 at 15:53
  • $\begingroup$ Is there a reason why you wouldn't want the areas equal rather than the width being proportional to sample size? You might want to look at mathematica.stackexchange.com/questions/145810/…. $\endgroup$
    – JimB
    Jul 11, 2018 at 16:25

2 Answers 2

DistributionChart[data, Method -> {"BoxWidth" -> "Scaled"}]

makes the bar widths proportional to the square root of the data sizes.

The first example in DistributionChart >> Options >> Method:

data = Table[RandomReal[NormalDistribution[], i], {i, {100, 400, 900, 1600}}];
 Method -> {"BoxWidth" -> "Scaled"}, 
 ChartLabels -> Length /@ data]

enter image description here

  • $\begingroup$ I would like further control over the violin chart preferably being able to control the SmoothKernelDistribution directly. In my case the problem is that the data provided is in integer form even though the underlying values are random reals. I would like the Method to allow for direct control of SmoothKernelDistribution. In my case I would like Method-> Function[x,SmoothKernelDistribution[x,1] so that the selected data is fed to this function. $\endgroup$
    – NigelKg
    Oct 27, 2022 at 11:30

I know this isn't what you asked. But for a comparison among violin plots where the distribution shape and the sample size affect what you see, I'm not sure how one would interpret differences.

If you want to compare shapes of the distributions a much more direct approach is the following:

data1 = RandomVariate[NormalDistribution[-1, 1], 1000];
data2 = RandomVariate[NormalDistribution[0, 3], 200];
data3 = RandomVariate[LogNormalDistribution[0, 0.6], 500];
skd1 = SmoothKernelDistribution[data1];
skd2 = SmoothKernelDistribution[data2];
skd3 = SmoothKernelDistribution[data3];
Plot[{PDF[skd1, x], PDF[skd2, x], PDF[skd3, x]},
 {x, Min[data1, data2, data3], Max[data1, data2, data3]},
 PlotRange -> {All, All}, 
 PlotLegends -> {"Data 1", "Data 2", "Data 3"}]

Overlaid densities


Your Answer

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

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