# Why are these violin plots "clipped"?

**URL:** https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006
**Category:** Visualization
**Tags:** plotting, plots, statsplots
**Created:** [August 20, 2023, 4:07pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006 "2023-08-20T16:07:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [August 20, 2023, 4:07pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/1 "2023-08-20T16:07:17Z")

</div>

Hi all, I produced some violin plots from some data I have:

```julia
df_temp = DataFrame(mnemonic = df_model_master[:, :mnemonic],
    instruction = df_model_master[:, instruction_sym],
    shift = df_model_master[:, has_barrel_shift_sym],
    value = df_model_master[:, mean_power_sym] .* u"W"
)
selected_instructions = ["add", "uxtb", "uxth"]
df_filtered = filter(:mnemonic => m -> m ∈ selected_instructions, df_temp)
df_pos = filter(:shift => identity, df_filtered)
df_neg = filter(:shift => x -> !x, df_filtered)

p = violin(0;
)
p = @df df_neg violin!(:mnemonic, :value;
    # xlabel = "Instructions",
    ylabel = "Measured power",
    side = :left, label = "not active",
    left_margin = 5mm,
    bottom_margin = 3mm,
    size = (800, 500),
    tickfontsize = 14,
    guidefontsize = 16

)
p = @df df_pos violin!(:mnemonic, :value;
    # xlabel = "Instructions",
    ylabel = "Measured power",
    side = :right, label = "active",
    left_margin = 5mm,
    bottom_margin = 3mm,
    size = (800, 500),
    tickfontsize = 14,
    guidefontsize = 16

)

```

What I obtain is this:

 ![violin_plot_selected](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c0e1bc51ebede5ed382fdb1efcde902b054bf27.png)  
As you see the violins are a bit… “clipped” at the ends (top and bottom)? Why is it so? And can I “smoothen” them? Thanks!

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 20, 2023, 5:59pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/2 "2023-08-20T17:59:16Z")

</div>

Violin plots are always clipped (I think to the range of the input data, or maybe plus some margin?), because the function that defines the curve is the sum of Gaussian distributions, which have full support.

The clipping is more obvious for data like yours that is not well approximated by a normal distribution.

Python does the same thing:

```python
import matplotlib.pyplot as plt
plt.violinplot([[3, 3, 3, 3, 7, 7, 7, 7], [2, 2, 4, 4]])

```

![violin](https://global.discourse-cdn.com/julialang/original/3X/4/0/40b2e6faf0cc39aa0cbdf82667e021f06200695c.png)

As your script doesn’t include the import statements, I can’t tell which plotting library you are using, but check the documentation for the `violin!` function to see if there is a scale parameter that will let you reduce the standard deviation of the Gaussians. That will create a less visible clipped appearance.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [August 20, 2023, 6:23pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/3 "2023-08-20T18:23:08Z")

</div>

With GMT they are not clipped.

[https://www.generic-mapping-tools.org/GMTjl\_doc/examples/plotting\_functions/05\_1\_stats/#violin\_plots](https://www.generic-mapping-tools.org/GMTjl_doc/examples/plotting_functions/05_1_stats/#violin_plots)

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 20, 2023, 9:24pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/4 "2023-08-20T21:24:36Z")

</div>

Sure they are:

```julia
using GMT
violin(rand((2, 3, 11), 10, 3), fill=true, show=true)

```

 ![violin](https://global.discourse-cdn.com/julialang/original/3X/4/1/418dceb5c7f6417d70d0aa92aed8cc01cc4e6020.png)

This is a fundamental property of violin plots.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [August 20, 2023, 10:10pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/5 "2023-08-20T22:10:26Z")

</div>

> [@maxkapur](#):
>
> This is a fundamental property of violin plots.

I don’t think it’s really fundamental; e.g., with Makie you can choose. Translating [the example above](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/2) to Makie syntax,

```julia
using CairoMakie
violin([fill(1, 8); fill(2, 4)], [3, 3, 3, 3, 7, 7, 7, 7, 2, 2, 4, 4])

```

gives

 ![violins](https://global.discourse-cdn.com/julialang/original/3X/e/e/eeb2b19ce2f2cf4760462a20295f0afd03df7d02.png)  
while

```julia
violin([fill(1, 8); fill(2, 4)], [3, 3, 3, 3, 7, 7, 7, 7, 2, 2, 4, 4]; datalimits=extrema)

```

gives

 ![violins_clipped](https://global.discourse-cdn.com/julialang/original/3X/f/b/fba589359f476ef199d9b255a25f61c082433529.png)

---

<div class="post-metadata">

### Author: ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)
#### Post date: [August 20, 2023, 10:16pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/6 "2023-08-20T22:16:35Z")

</div>

This is pedantic, but even those are clipped, just at a place on the Gaussian curve where the function is already close to zero 🙂

(And reasonable people may disagree whether those plots are a better representation of the underlying distribution than the more tightly clipped ones.)

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [August 20, 2023, 10:26pm UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/7 "2023-08-20T22:26:49Z")

</div>

You caught me with that one (would need to check why it’s doing it) but the examples that I linked above show no clipping. Or, as you said, clipp the density function very close to zero.

---

<div class="post-metadata">

### Author: ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)
#### Post date: [August 21, 2023, 9:09am UTC](https://discourse.julialang.org/t/why-are-these-violin-plots-clipped/103006/8 "2023-08-21T09:09:37Z")

</div>

Thanks to all! So I guess the true underlying answer is that my data set is too little to appreciate a pleasure looking violin plot. No problem though, if it’s supposed to look this way.
