Problem creating violin plots

Hi all,

I’m new to Julia and I’m hoping to make some violin plots.

I recently had version 1.1.1 downloaded and I found some sample code to create a violin plot

using StatsPlots
y = rand(100, 4) # Four series of 100 points each
violin(["Series 1" "Series 2" "Series 3" "Series 4"], y, leg = false)

When I ran the code above, I kept getting a collection of errors suggesting that some packages were out of date and whatnot. So I uninstalled and re-installed the latest version of Julia available today (1.4.2) and added IJulia so I could work within a Jupyter Notebook.

I tried running the same code above and got this error

MethodError: no method matching get_color_palette(::Symbol, ::RGBA{Float64}, ::Int64)
Closest candidates are:
  get_color_palette(::Any, ::Any) at C:\Users\jguas\.julia\packages\PlotUtils\nCtbM\src\colorschemes.jl:328
  get_color_palette(!Matched::ColorGradient, ::Any) at C:\Users\jguas\.julia\packages\PlotUtils\nCtbM\src\colorschemes.jl:329

Stacktrace:
 [1] _update_subplot_colors(::Plots.Subplot{Plots.GRBackend}) at C:\Users\jguas\.julia\packages\Plots\WwFyB\src\args.jl:1436
 [2] _update_subplot_args(::Plots.Plot{Plots.GRBackend}, ::Plots.Subplot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Int64, ::Bool) at C:\Users\jguas\.julia\packages\Plots\WwFyB\src\args.jl:1523
 [3] _subplot_setup(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Array{Dict{Symbol,Any},1}) at C:\Users\jguas\.julia\packages\Plots\WwFyB\src\pipeline.jl:297
 [4] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{String,2},Array{Float64,2}}) at C:\Users\jguas\.julia\packages\Plots\WwFyB\src\plot.jl:206
 [5] plot(::Array{String,2}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:leg, :seriestype),Tuple{Bool,Symbol}}}) at C:\Users\jguas\.julia\packages\Plots\WwFyB\src\plot.jl:57
 [6] violin(::Array{String,2}, ::Vararg{Any,N} where N; kw::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol},NamedTuple{(:leg,),Tuple{Bool}}}) at C:\Users\jguas\.julia\packages\RecipesBase\G4s6f\src\RecipesBase.jl:393
 [7] top-level scope at In[13]:2

I’ve done some googling and searching here and can’t find a good fix. Anyone have any idea what might be going on?

Are you loading another plotting package in addition to StatsPlots? Also, are you executing the code from the default environment?

Hi there!

Thanks for the quick reply – No other plotting packages are loaded (though ‘CSV’ is, but that’s to get my own data into Julia), and yes, I’m running from the default environment via jupyter notebooks.

Hmmm, I’m not able to reproduce the error. Have you tried running this in VS Code or Atom/Juno?

I can’t reproduce this error either, the code snippet you posted works as expected.

Can you post the output of versioninfo() and ]st?

I’m also having problems with this sample code (which comes from the Plots documentation at Tutorial · Plots)

using StatsPlots
y = rand(100, 4) # Four series of 100 points each
violin(["Series 1" "Series 2" "Series 3" "Series 4"], y, leg = false)

When I run it, I get the following error:

ERROR: Expects 1 elements in each col of y, found 100.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] _compute_xyz(::Array{String,1}, ::Array{Float64,1}, ::Nothing) at /home/adriano/.julia/packages/RecipesPipeline/uPBKQ/src/series.jl:98
 [3] macro expansion at /home/adriano/.julia/packages/RecipesPipeline/uPBKQ/src/series.jl:164 [inlined]
 [4] apply_recipe(::AbstractDict{Symbol,Any}, ::Type{RecipesPipeline.SliceIt}, ::Any, ::Any, ::Any) at /home/adriano/.julia/packages/RecipesBase/92zOw/src/RecipesBase.jl:282
 [5] _process_userrecipes!(::Any, ::Any, ::Any) at /home/adriano/.julia/packages/RecipesPipeline/uPBKQ/src/user_recipe.jl:36
 [6] recipe_pipeline!(::Any, ::Any, ::Any) at /home/adriano/.julia/packages/RecipesPipeline/uPBKQ/src/RecipesPipeline.jl:70
 [7] _plot!(::Plots.Plot, ::Any, ::Any) at /home/adriano/.julia/packages/Plots/vsE7b/src/plot.jl:172
 [8] #plot#129 at /home/adriano/.julia/packages/Plots/vsE7b/src/plot.jl:58 [inlined]
 [9] violin(::Any, ::Vararg{Any,N} where N; kw::Any) at /home/adriano/.julia/packages/RecipesBase/92zOw/src/RecipesBase.jl:403
 [10] top-level scope at REPL[6]:1

Am I missing something here?

Note that this is a different error from the one reported above, so probably worth opening a separate thread.

That said I had a look and this time around can reproduce this. It seems to me the issue is that the documentation you link to is the Plots documentation (not StatsPlots, which is a separate package), and this is an old example of how to call violin that was used here as an example for how recipes work, not to document the violin function.

To work out how it should be called it’s usually easiest to use the REPL help:

help?> violin
search: violin violin!

  violin(x,y,z)
  violin!(x,y,z)

  Make a violin plot.

  Example
  ≡≡≡≡≡≡≡≡≡

  julia> violin(repeat([1,2,3],outer=100),randn(300))

so the key here is that the groups have the same length as the data you pass in - hence why your error says “expected 1 element in y”, your groups only occur once each in ["Series 1" "Series 2" "Series 3" "Series 4"]. So to get this to run, you should repeat the groupings like this:

julia> using StatsPlots

julia> violin(repeat(["Series 1" "Series 2" "Series 3" "Series 4"], 100), rand(100, 4), leg = false)

The “documented” usage of violin is actually using the @df macro on a DataFrame directly: https://github.com/JuliaPlots/StatsPlots.jl#boxplot-dotplot-and-violin

EDIT: I’ve made a PR to edit the docs as this seems to be an error https://github.com/JuliaPlots/PlotDocs.jl/pull/225

1 Like

Sorry for hijacking this thread with a different problem. I blame it on working too late at night :frowning:

In Julia, I have always used PyPlot directly. This is my first time using Plots. That’s why I was working from the Plots documentation. Then, when I tried the boxplot() and violin() examples, I got an error message saying that these plots had been moved to the StatsPlots package. I then installed the StatsPlots package and ran into the error I reported here.

Thanks a lot for your detailed response. That did solve my problem, although I don’t understand the need for repeating the xticks.

No worries - as you see there is already discussion on the PR I made as to whether the old behaviour (specifying one group per item in the second dimension) should be brought back!