3

创建一个小提琴情节的正确方法是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')

小提琴情节示例

但这是正确的方法吗?

4

1 回答 1

5

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

而是使用重复的标签,例如

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()

在此处输入图像描述

于 2018-02-15T16:32:51.400 回答