[英]Average point and standard deviation bars on scatter plot
If I have a scatter plot like this MWE: 如果我有像这样的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)
what's the easiest way to plot the 'average point' (is there a proper word for that?) on the plot? 在图上绘制“平均点”(是否有合适的词?)的最简单方法是什么? All of the tutorials and examples I've found show how to plot the line of best fit, but I just want the single point.
我发现的所有教程和示例都显示了如何绘制最佳拟合线,但我只想单点。
Plotting (x_ave, y_ave)
works, but is there a better way, especially since I'd eventually want to show the standard deviations with error bars too? 绘制
(x_ave, y_ave)
是(x_ave, y_ave)
,但是有没有更好的方法,特别是因为我最终还是想显示带有误差线的标准偏差?
If you want to plot a single scatter point with error bars, the best way would be to use the errorbar
module. 如果要绘制带有误差线的单个散点,最好的方法是使用
errorbar
模块。 The following answer shows an example of using it with customized properties of error bars and the average point with a standard deviation of 1 for both x and y. 以下答案显示了将其与误差条的自定义属性以及x和y的平均点的标准偏差均为1一起使用的示例。 You can specify your actual standard deviation values in
xerr
and yerr
. 您可以在
xerr
和yerr
指定实际的标准偏差值。 The error bars can be removed from the legend using this solution. 使用此解决方案可以从图例中删除误差线。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.