admin管理员组

文章数量:1315999

I have a dataframe for which I am plotting violin plot. The dataframe has two categories A and B and highly imbalance data (one df has 1000 samples/rows and other has 200). I want to compare both categories and I believe looking at violin plot will give more information about distribution. While plotting it, I can plot two violins for separate groups. However, I want left side to represent category A while right side represent category B. I am able to plot it but what I also want is to keep the boxes, whiskers interquartile ranges etc. to be separate for each group. My plot has just one box in center (image 1). I want something like as is in image 2 (ref: /).

Setting split=False is creating two separately voilins.

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

# Sample data for df1 and df2
df1 = pd.DataFrame({
    'category': ['A', 'A', 'B', 'B', 'B'],
    'Value': [10, 200, 300, 40, 50]
})

df2 = pd.DataFrame({
    'category': ['A', 'A', 'B', 'B', 'B'],
    'Value': [3, 4, 5, 4, 1]
})

# Add group labels to each dataframe
df1['day'] = 'thurs'
df2['day'] = 'thurs'

# Combine the dataframes
combined_df = pd.concat([df1, df2])
# Plot violin chart
plt.figure(figsize=(10, 6))
sns.violinplot(x='day', y='Value', hue = 'category', data=combined_df, split=True)
plt.title('Violin Plot of df1 and df2')
plt.show()

本文标签: pythonViolin plotConcatenating two violins in oneStack Overflow