I want to create (eg) violinplots from pandas dataframes which can belong to multiple categories, ideally in a single figure. Not sure how to go about this however -- any suggestions? Many thanks!
A simple example showing separate plots. Here, x is main grouping variable, y are the data to be grouped and z defines membership/category. For simplicity, I've just set z to an integer to [0,1,2] randomly.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# dummy data
np.random.seed(12345)
x = np.random.randint(1,6,1000)
y = np.random.randn(1000)
z = np.random.randint(0,3,1000)
df = pd.DataFrame(data=np.array([x,y,z]).T,columns=['x','y','z'])
All data (for verification?):
sns.violinplot(x='x',y='y',data=df)
plt.title('all data')
Violin plot of all data regardless of Z
Individual plots:
fig,ax = plt.subplots(nrows=3,ncols=1,sharex=True)
sns.violinplot(x='x',y='y',data=df.loc[df['z']<=0],ax=ax[0])
ax[0].set_title('z <= 0')
sns.violinplot(x='x',y='y',data=df.loc[df['z']<=1],ax=ax[1])
ax[1].set_title('z <= 1')
sns.violinplot(x='x',y='y',data=df.loc[df['z']<=2],ax=ax[2])
ax[2].set_title('z <= 2')
plt.tight_layout();
3 violin plots of data with z<=[0,1,2] respectively
What I'd like is a plot that looks like the following, except that 'z' uses the grouping of the above plot:
plt.figure()
sns.violinplot(x='x',y='y',data=df,hue='z');
Violin plot using 'hue' where only data with z==[0,1,2] is grouped for each color
