使用seaborn绘制单个数据点

5

我正在使用seaborn创建一个箱线图。但是如何添加一条线或单个点以显示图表上的单个数据值呢?例如,我该如何在下面的图表中绘制值为3.5的数据。

import seaborn as sns
import matplotlib.pyplot as plt

df1 = [2.5, 2.5, 2, 3, 4, 3.5]
sns.set_style("whitegrid")
fig, ax = plt.subplots(figsize=(8,8))
plot1 = sns.boxplot(ax=ax, x=df1, linewidth=5)
2个回答

13

您可以使用plt.scatter来实现相同的效果。个人偏好,因为语法更短更简洁:

df1 = [2.5, 2.5, 2, 3, 4, 3.5]
sns.set_style("whitegrid")
fig, ax = plt.subplots(figsize=(8,8))
plot1 = sns.boxplot(ax=ax, x=df1, linewidth=5)

plt.scatter(3.5, 0, marker='o', s=100)

1
这是期望的答案。它更简洁、更清晰,更突出重点。 - Sam Murphy

4

这是您正在寻找的内容吗?

df1 = [2.5, 2.5, 2, 3, 4, 3.5]
sns.set_style("whitegrid")
fig, ax = plt.subplots(figsize=(8,8))
sns.boxplot(ax=ax, x=df1, linewidth=5)

# sns scatter, with regression fit turned off
sns.regplot(x=np.array([3.5]), y=np.array([0]), scatter=True, fit_reg=False, marker='o',
            scatter_kws={"s": 100})  # the "s" key in `scatter_kws` modifies the size of the marker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接