我有一个类似的数据集
x y
0.07 0.400000
0.07 0.171429
0.08 0.214286
0.08 0.214286
0.08 0.214286
0.09 0.142857
0.09 0.571429
0.09 0.071429
0.09 0.271429
0.10 0.342857
我想绘制给定范围x的小提琴图,例如从0.07到0.08,然后从0.09到0.1
我正在使用
ax = sns.violinplot(x="x", y="y", data=df)
显然,这给了我x值的小提琴图。使用上面的数据,我将得到4个图。
答案 0 :(得分:2)
您可以尝试将熊猫切成薄片将数据放入垃圾箱。可以将这些垃圾箱添加到新列中:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({'x': np.random.randint(6, 13, 50) * 0.01,
'y': np.random.uniform(0, 1, 50)})
ranges = np.arange(0.055, 0.14, 0.02)
ax = sns.violinplot(x=pd.cut(df.x, ranges), y='y', data=df)
ax.set_xticklabels([f'{r + 0.005:.2f}-{r + 0.015:.2f}' for r in ranges[:-1]])
plt.show()
PS:适应注释中的其他问题:
df = pd.DataFrame({'x': np.random.randint(6, 13, 50) * 0.01,
'y': np.random.uniform(0, 1, 50)})
ranges = np.append(0.055, np.arange(0.065, 0.14, 0.02))
df['category'] = pd.cut(df.x, ranges)
counts = df.groupby(['category'])['x'].count()
ax = sns.violinplot(x='category', y='y', data=df, palette='Greens')
labels = ['0.06'] + [f'{r + 0.005:.2f}-{r + 0.015:.2f}' for r in ranges[1:-1]]
ax.set_xticklabels([f'{label}\n({count / sum(counts) * 100:.1f} %)' for label, count in zip(labels, counts)])
plt.tight_layout()
plt.show()
要在小提琴上添加百分比:
counts = df.groupby(['category'])['x'].count()
means = df.groupby(['category'])['y'].mean()
for i, (mean, count) in enumerate(zip(means, counts)):
ax.text(i, mean, f'{count/sum(counts)*100} %', ha='center', va='center', color='r')