创建一个小提琴图的正确方法是什么,其中一把小提琴按色调分开?

What is the correct way to create a violin plot that has one violin split by hue?

创建将一把小提琴分割成 hue 的小提琴图的正确方法是什么?

我尝试了不同的方法,似乎唯一的方法是为数据集中的每个条目创建一个共享相同值的特征。并将该功能的名称作为 x.

传递
fig = plt.figure(figsize=(20, 8))

fig.add_subplot(1, 3, 1)
ax = sns.violinplot(x='feature', y='height',
              data=train_cleansed_height,
              scale='count',
              hue='feature', split=True,
              palette='seismic',
              inner='quartile')

fig.add_subplot(1, 3, 2)
ax = sns.violinplot(x='workaround', y='height',
              data=train_cleansed_height,
              scale='count',
              hue='feature', split=True,
              palette='seismic',
              inner='quartile')

fig.add_subplot(1, 3, 3)
ax = sns.violinplot(x=None, y='height',
              data=train_cleansed_height,
              scale='count',
              hue='feature', split=True,
              palette='seismic',
              inner='quartile')
plt.xlabel('x=None')

但这是正确的方法吗?

seaborn.violinplotx 参数需要是位置的数据。如果需要单个位置,则 x 的数据需要包含唯一值。如果为 xhue 选择相同的数据,x 将被赋予两个不同的唯一值,因此选择了两个位置,如第一个图中所示。

而是使用像

这样的重复标签
sns.violinplot(x=["some label"]*len(df),  ...) 

在单个位置创建小提琴图。

import numpy as np;np.random.seed(1)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

a = np.concatenate((np.random.binomial(3,0.3,50)*2.2+1, np.random.rayleigh(3,50)))
df = pd.DataFrame({"height" : a, "feature" : ["A"]*50+["B"]*50})

fig = plt.figure()

fig.add_subplot(1, 2, 1)
ax = sns.violinplot(x='feature', y='height',
              data=df,
              scale='count',
              hue='feature', split=True,
              palette='seismic',
              inner='quartile')

fig.add_subplot(1, 2, 2)
ax = sns.violinplot(x=["AB"]*len(df), y='height',
              data=df,
              scale='count',
              hue='feature', split=True,
              palette='seismic',
              inner='quartile')

plt.tight_layout()
plt.show()