0

I have a matplotlib figure with a few violinplots on it (although this question would apply to any similar plot or other dataframe situation, not just violinplots). I currently run my code and it spits out the figure, with one violinplot per category. The code looks something like the following:

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

df = pd.DataFrame(data=np.random.random_integers(low=0,high=1000,size=(100,1)),
                  columns=['row0']
                  )

df['r0_range']='temp' #create a new column 'r0_range', give it a preliminary value
#make assignments depending on value of row0
df['r0_range'][df['row0']<=250]='[0,250]'
df['r0_range'][df['row0']>250]='(250,500]'
df['r0_range'][df['row0']>500]='(500,750]'
df['r0_range'][df['row0']>750]='(750,1000]'

fig1, ax1 = plt.subplots(1,1)

ax1 = sns.violinplot(data=df, x='r0_range', y='row0', inner=None, ax=ax1)

Which pops out the following:

violin plots with four violins

I want to include on my figure a fifth violinplot that represents all of the data in all of the categories. Is there an elegant way to do that without having to copy the row0 data into new rows of the dataframe?

Emily Beth
  • 709
  • 1
  • 7
  • 23

1 Answers1

0

Perhaps something like this will do what you are looking for:

df = pd.DataFrame(data=np.random.randint(0, 1001, 100), columns=['row0'])
g = df.groupby(pd.cut(df['row0'], [0, 250, 500, 750, 1000]))
for name, data in g.groups.items():
    df[name] = df.loc[data]['row0']
sns.violinplot(data=df, inner=None, ax=ax1)

enter image description here

fuglede
  • 17,388
  • 2
  • 54
  • 99