1

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)

enter image description here

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?

Jim421616
  • 1,434
  • 3
  • 22
  • 47

1 Answers1

2

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)

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thanks for that. Your solution works, but I get an error: `handles = [h[0] for h in handles] TypeError: 'PathCollection' object does not support indexing` What does this mean? – Jim421616 Apr 30 '19 at 00:58
  • @Jim421616: Are you find without these last three lines? If yes, then you can just put `plt.legend()` after `plt.errorbar(...)`. In this case, you will have errorbars too in the legend. I am off to sleep now so will have a look tomorrow if you need the legend without the errorbar. Else, you can try the solutions on the link I posted for legends in my answer and similar other answers on stack overflow – Sheldore Apr 30 '19 at 01:02