我想在swarmplot上面画一个“高亮”的点,就像这样

swarmplot没有y轴,所以我不知道如何绘制那个点。
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])发布于 2020-04-01 19:48:17
这种方法的前提是知道您希望突出显示的数据点的索引,但它应该可以工作-尽管如果您在单个Axes实例上有多个swarmplot,它将变得稍微复杂一些。
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-09-16 20:00:48
如果为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()

https://stackoverflow.com/questions/60969052
复制相似问题