0

I'm trying to create a violinplot using matplotlib (but I could use seaborn, but I have almost no experience with it). My challenge is that the categorical variable I have is actually numeric, and matplotlib seems to insist on mapping the categorical variables to integers. I could get around this by relabeling the axis, but in my case the variables aren't evenly spaced out. So I need the violinplots to be unevenly spread out. Here is an example where I use exponential distributions with rates 0.5, 1, 2, and 4. These are obviously not evenly spread out, and I'd like to be able to plot them so that the horizontal axis is the 'rate', and the axis is on a linear scale.

import matplotlib
import random

data = []
for rate in [0.5, 1, 2, 4]:
    data.append([])
    for counter in range(1000):
        data[-1].append(random.expovariate(rate))
fig, ax = plt.subplots()
ax.violinplot(data)

This produces:

enter image description here

Of course, my real data is not this simple, but I think this illustrates what I'm after well enough.

Joel
  • 22,598
  • 6
  • 69
  • 93

0 Answers0