python - seaborn 中的一半(不分裂!)小提琴情节
问题描述
目前,seaborn根据一个变量通过设置来提供分割小提琴图的功能。我想制作一个“半”小提琴情节,即每把小提琴的一半被省略的情节。这样的图描绘了类似于每个连续变量的 pdf 的内容,仅绘制在每个分类变量的每条垂直线的一侧。split=True
hue
我已经设法seaborn
用绘制的值范围之外的额外数据点和额外的虚拟色调来绘制这个,但我想知道这是否可以在不实际更改数据集的情况下完成,例如在sns.violinplot()
参数内。
例如,这张图:
由此代码段创建:
# imports
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# load dataset from seaborn
datalist = sns.get_dataset_names()
dataset_name = 'iris'
if dataset_name in datalist:
df = sns.load_dataset(dataset_name)
else:
print("Dataset with name: " + dataset_name + " was not found in the available datasets online by seaborn.")
# prepare data
df2 = df.append([-999,-999,-999,-999,'setosa'])
df2['huecol'] = 0.0
df2['huecol'].iloc[-1]= -999
# plot
fig = plt.figure(figsize=(6,6))
sns.violinplot(x='species',y="sepal_width",
split=True, hue ='huecol', inner = 'quartile',
palette="pastel", data=df2, legend=False)
plt.title('iris')
# remove hue legend
leg = plt.gca().legend()
leg.remove()
plt.ylim([1,5.0])
plt.show()
解决方案
答案很简单,不,如果不诱使 seaborn 认为有hue
礼物,这是不可能的。
这个答案显示了如何在 matplotlib 中执行此操作,原则上同样可以应用于 seaborn violinplots,即切掉一半的小提琴路径。
推荐阅读
- r - 使用 dplyr 改变一个新列,其值基于多个条件;尝试了 lapply 但仍然无法正常工作
- python - 比较 Pandas 中不同列对中的值
- python - Pyspark 分类数据矢量化,具有与之相关的数值
- sql - 测试 ARRAY 是否包含给定 ARRAY 的 ALL 或 NO 元素
- google-analytics - 如何更改谷歌分析中的网站网址路径?
- java - Java 执行时间不加起来
- sql-server - 从 Excel 粘贴时 SQL Server 显示 Null,即使单元格中有值
- mysql - 无法从终端登录 mysql 上的任何帐户
- visual-studio - 是否可以在 Developer Powershell Terminal 中更改字体?
- docker-compose - 如何在启动时在 kong docker 容器中运行 curl 命令?