1

I want to create a figure with different violin plots on the same graph (but not on the same column). My data are a list of dataframes and I want to create a violin plot of one column for each dataframe. (the names of the columns in the final figure I prefer to have as a name that is inside each dataframe in one other column).

I used this code:

for i in range(0,len(sta_list)):
    plt.violinplot(sta_list[i]['diff_APS_1'])

I know that this is wrong, I want to split up the resulting plots in the figure.

enter image description here

My Work
  • 2,143
  • 2
  • 19
  • 47
kino
  • 41
  • 4

1 Answers1

0

You can specify the x-position of the violin plot for each column using positions argument

for i in range(0, len(sta_list)):
    plt.violinplot(sta_list[i]['diff_APS_1'], positions=[i])

A sample answer for demonstration taking the dataset from this post

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


x = np.random.poisson(lam =3, size=100)
y = np.random.choice(["S{}".format(i+1) for i in range(4)], size=len(x))
df = pd.DataFrame({"Scenario":y, "LMP":x})

fig, ax = plt.subplots()

for i, key in enumerate(['S1', 'S2', 'S3', 'S4']):
    ax.violinplot(df[df.Scenario == key]["LMP"].values, positions=[i])

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71