python - 散点图上的平均点和标准差条

标签 python matplotlib scatter-plot

如果我有像这样的 MWE 散点图:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(5)
fig = plt.figure()
ax = fig.add_subplot(111)
xlist = []
ylist = []
for i in range(500):
    x = np.random.normal(100)
    xlist.append(x)
    y = np.random.normal(5)
    ylist.append(y)

x_ave = np.average(x)
y_ave = np.average(y)
plt.scatter(xlist, ylist)
plt.scatter(x_ave, y_ave, c = 'red', marker = '*', s = 50)

enter image description here

在绘图上绘制“平均点”(有合适的​​词吗?)的最简单方法是什么?我找到的所有教程和示例都展示了如何绘制最佳拟合线,但我只想要单点。

绘制(x_ave, y_ave)是可行的,但是有没有更好的方法,特别是因为我最终也想用误差线显示标准差?

最佳答案

如果您想绘制带有误差线的单个散点,最好的方法是使用 errorbar模块。以下答案显示了一个将其与误差线的自定义属性以及 x 和 y 的标准差均为 1 的平均点一起使用的示例。您可以在 xerryerr 中指定实际标准差值。可以使用this从图例中删除误差线。解决方案。

plt.scatter(xlist, ylist)

plt.errorbar(x_ave, y_ave, yerr=1, xerr=1, fmt='*', color='red', ecolor='black', ms=20, 
             elinewidth=4, capsize=10, capthick=4, label='Average')

handles, labels = ax.get_legend_handles_labels()
handles = [h[0] for h in handles]
ax.legend(handles, labels, loc='best', fontsize=16)

enter image description here

关于python - 散点图上的平均点和标准差条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912292/

相关文章:

python - 选择更新查询 : Lock wait timeout exceeded error

python - 如何在包含每小时平均值以及每小时每个平均值的相应计数的数据帧末尾添加新列。

由于格式值错误,excel散点图不正确

r - 在 ggplot2 中手动为 x 和 y Axis 添加中断

python - 使用模型方法作为 Django 模型的默认值?

javascript - Python:发送 JSON 数据时使用 POST 请求的 FastAPI 错误 422

python - matplotlib 中的阶跃函数

matplotlib - 设置固定颜色条值

python - Windows - Cygwin - Vim - Python - 导入错误

iphone - 如何使用核心图更改部分散点图的线条颜色?