Positioning in violin plot

I would like to add ground truth values to a violin plot. So suppose I knew that for my category "a" the true value is 2.0 and I have a violin plot, I could use scatter like so:

violin(repeat(["a", "b", "c"], outer=100), randn(300), leg=false)
scatter!([(0.5, 2.0)])

Now I used an absolute value (0.5) to determine the x-position of the relevant violin plot which works in this toy example. Unfortunately, with my data, the violin plots use a different spacing, so I cannot just use 0.5, 1.5, 2.5 ... as the x-positions.

How can I determine the correct x-position to add my ground truth value centered in the corresponding violin plot regardless of the shape of the data?



One method might be to exploit the fact that scatter! can use a vector of strings for the x-positions. So something like this:

using StatsPlots
myviolin = violin(repeat(["a", "b", "c"], outer=100), randn(300), leg=false)
scatter!(["a", "b", "c"], [2,1,0], markersize=10) # note the x-argument
savefig(myviolin, "violin_with_markers.png")


Does this work for your case?

1 Like

It does! What a simple and elegant solution, thanks a lot!