Search code examples
juliaviolin-plot

Violin plot problem in Julia: Cannot use same x-positions for 2 datasets


I use Julia Version 1.4.2 and want to make a violin plot, where I have two datasets A and B, which I want to plot using the same x-ticks. I want that data A is the left half of each x-position and B the right half.

Here a test example:

using StatsPlots
A=[rand(10), rand(12), rand(44)]
B=A./2
X=[1,2,3]
StatsPlots.violin(A,xticks=X,side=:left)
StatsPlots.violin!(B,xticks=X,side=:right)

When I run this, data A gets plotted as I wish, but data B is shifted. I would like that at x-position 1 there is A to the left and B to right to compare them next to each other. However, I get a plot where the data from B is at x-position 4-6 (without labelled ticks though):

violin plot


Solution

  • I am using a solution that works, but which is a bit ugly:

    using StatsPlots
    A=[rand(10), rand(12), rand(44)]
    B=A./2
    X=[[1],[2],[3]] #this is changed compared to my initial post
    
    StatsPlots.violin(X[1],A[1],side=:left)
    StatsPlots.violin!(X[1],B[1],side=:right)
    
    for i=2:length(X)
        println(length(X))
        StatsPlots.violin!(X[i],A[i],side=:left)
        StatsPlots.violin!(X[i],B[i],side=:right)
    
    end
    current()
    

    figure