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

Ily*_*nov 3 python matplotlib python-3.x seaborn violin-plot

创建一个小提琴图的正确方法是什么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')
Run Code Online (Sandbox Code Playgroud)

小提琴情节示例

但这是正确的方法吗?

Imp*_*est 5

x参数seaborn.violinplot需要是位置的数据。如果需要单个位置,则数据x需要由唯一值组成。如果为x和选择相同的数据huex则将获得两个不同的唯一值,因此选择了两个位置,如第一个图中所示。

而是使用重复的标签,如

sns.violinplot(x=["some label"]*len(df),  ...) 
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明