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)
但这是正确的方法吗?
的x
参数seaborn.violinplot
需要是位置的数据。如果需要单个位置,则数据x
需要由唯一值组成。如果为x
和选择相同的数据hue
,x
则将获得两个不同的唯一值,因此选择了两个位置,如第一个图中所示。
而是使用重复的标签,如
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)
归档时间: |
|
查看次数: |
2644 次 |
最近记录: |