Python 正确的方法是什么来创建一个小提琴情节,其中一个小提琴被色调分割?
创建一个小提琴图的正确方法是什么,该图中的一个小提琴按色调分割 我尝试过不同的方法,似乎唯一的方法是创建一个特性,该特性为数据集中的每个条目共享相同的值。并将该功能的名称传递为Python 正确的方法是什么来创建一个小提琴情节,其中一个小提琴被色调分割?,python,python-3.x,matplotlib,seaborn,violin-plot,Python,Python 3.x,Matplotlib,Seaborn,Violin Plot,创建一个小提琴图的正确方法是什么,该图中的一个小提琴按色调分割 我尝试过不同的方法,似乎唯一的方法是创建一个特性,该特性为数据集中的每个条目共享相同的值。并将该功能的名称传递为x fig = plt.figure(figsize=(20, 8)) fig.add_subplot(1, 3, 1) ax = sns.violinplot(x='feature', y='height', data=train_cleansed_height,
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.violinplot的
x
参数需要作为该位置的数据。如果需要单个位置,x
的数据需要由唯一值组成。如果为x
和hue
选择相同的数据,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()
中间图(“解决方案”)是否为所需结果?如果是这样,我认为你是正确的,你必须用一个x值欺骗seaborn。据我所知,
hue=
应该是在x=
之外使用的,而不是相反。在我看来也是这样。我只是想确定一下,因为在所有索引中传递具有相同值的Series对象感觉很奇怪。谢谢。有没有办法控制四分位线的颜色和线型?