Reputation: 1536
If I have a scatter plot like this 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?
Upvotes: 1
Views: 2242
Reputation: 39052
If you want to plot a single scatter point with error bars, the best way would be to use the errorbar
module. 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. You can specify your actual standard deviation values in xerr
and 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)
Upvotes: 2