gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:57:11 27 4
gpt4 key购买 nike

如果我有像这样的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com