# Problem creating violin plots

**URL:** <https://discourse.julialang.org/t/problem-creating-violin-plots/41298>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [June 12, 2020, 8:23pm UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298 "2020-06-12T20:23:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Guassi\_Moreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_guassi_moreira/32/15636_2.png) [@Joao\_Guassi\_Moreira](https://discourse.julialang.org/u/Joao_Guassi_Moreira)\
**Post date:** [June 12, 2020, 8:23pm UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/1 "2020-06-12T20:23:20Z")

</div>

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

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [June 12, 2020, 8:34pm UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/2 "2020-06-12T20:34:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joao\_Guassi\_Moreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_guassi_moreira/32/15636_2.png) [@Joao\_Guassi\_Moreira](https://discourse.julialang.org/u/Joao_Guassi_Moreira)\
**Post date:** [June 12, 2020, 9:12pm UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/3 "2020-06-12T21:12:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [June 13, 2020, 12:30am UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/4 "2020-06-13T00:30:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 13, 2020, 6:54am UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/5 "2020-06-13T06:54:46Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)\
**Post date:** [November 27, 2020, 1:11am UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/6 "2020-11-27T01:11:59Z")

</div>

I’m also having problems with this sample code (which comes from the Plots documentation at [Tutorial · Plots](https://docs.juliaplots.org/latest/tutorial/#Plot-Recipes-and-Recipe-Libraries))

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 27, 2020, 7:53am UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/7 "2020-11-27T07:53:16Z")

</div>

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:

```julia
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
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](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](https://github.com/JuliaPlots/PlotDocs.jl/pull/225)

---

<div class="post-metadata">

**Author:** ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)\
**Post date:** [November 27, 2020, 11:49am UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/8 "2020-11-27T11:49:01Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 27, 2020, 12:19pm UTC](https://discourse.julialang.org/t/problem-creating-violin-plots/41298/9 "2020-11-27T12:19:28Z")

</div>

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!
