SaiyanGirl
SaiyanGirl

Reputation: 17024

How to take draw an average line for a scatter plot

My data is the following:

x = [3,4,5,6,7,8,9,9]
y = [6,5,4,3,2,1,1,2]

And I can obtain the following two graphs.

enter image description here

and

enter image description here

However, what I want is this (an average of all the points along the way): enter image description here

Is it possible in matplotlib? Or do I have to change the list manually and somehow create:

x = [3,4,5,6,7,8,9]
y = [6,5,4,3,2,1,1.5]

RELEVANT CODE

ax.plot(x, y, 'o-', label='curPerform')
x1,x2,y1,y2 = ax.axis()
x1 = min(x) - 1 
x2 = max(x) + 1
ax.axis((x1,x2,(y1-1),(y2+1)))

Upvotes: 22

Views: 102344

Answers (2)

ryanjdillon
ryanjdillon

Reputation: 18948

This can done by generating a new y_mean from your data, then plotting this on the same plot axis using an additional call to ax.plot(), where:

  • x is the same x used in your scatter plot
  • y is an iterable with "mean" value you calculate repeated so that its length is equal to x, i.e. y_mean = [np.mean(y) for i in x].

Example:

import matplotlib.pyplot as plt
import random
import numpy as np


# Create some random data
x = np.arange(0,10,1)
y = np.zeros_like(x)    
y = [random.random()*5 for i in x]

# Calculate the simple average of the data
y_mean = [np.mean(y)]*len(x)

fig,ax = plt.subplots()

# Plot the data
data_line = ax.plot(x,y, label='Data', marker='o')

# Plot the average line
mean_line = ax.plot(x,y_mean, label='Mean', linestyle='--')

# Make a legend
legend = ax.legend(loc='upper right')

plt.show()

Resulting figure: enter image description here

Upvotes: 39

BrenBarn
BrenBarn

Reputation: 251373

Yes, you must do the calculation yourself. plot plots the data you give it. If you want to plot some other data, you need to calculate that data yourself and then plot that instead.

Edit: A quick way to do the calculation:

>>> x, y = zip(*sorted((xVal, np.mean([yVal for a, yVal in zip(x, y) if xVal==a])) for xVal in set(x)))
>>> x
(3, 4, 5, 6, 7, 8, 9)
>>> y
(6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 1.5)

Upvotes: 7

Related Questions