1

我想像这样在 swarmplot 上绘制一个“突出显示”的点

在此处输入图像描述

swarmplot 没有 y 轴,所以我不知道如何绘制该点。

import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])
4

2 回答 2

1

如果为 y 轴添加分组变量(使它们显示为单个组),则可以使用 hue 属性突出显示一个点,然后使用另一个变量突出显示您感兴趣的点。

然后您可以删除 y 标签以及样式和图例。

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid")

# Get data and mark point you want to highlight
tips = sns.load_dataset("tips")
tips['highlighted_point'] = 0
tips.loc[tips[tips.total_bill > 50].index, 'highlighted_point'] = 1

# Add holding 'group' variable so they appear as one
tips['y_variable'] = 'testing'

# Use 'hue' to differentiate the highlighted point
ax = sns.swarmplot(x=tips["total_bill"], y=tips['y_variable'], hue=tips['highlighted_point'])

# Remove legend
ax.get_legend().remove()

# Hide y axis formatting 
ax.set_ylabel('')
ax.get_yaxis().set_ticks([])
plt.show()

输出图

于 2020-09-16T12:00:48.017 回答
1

这种方法基于知道您希望突出显示的数据点的索引,但它应该可以工作 - 尽管如果您在单个实例上有多个 swarmplots,Axes它会变得稍微复杂一些。

import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])
artists = ax.get_children()
offsets = []
for a in artists:
    if type(a) is matplotlib.collections.PathCollection:
        offsets = a.get_offsets()
        break
plt.scatter(offsets[50,0], offsets[50,1], marker='o', color='orange', zorder=10)

在此处输入图像描述

于 2020-04-01T11:48:17.130 回答