Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take draw an average line for a scatter / a plot in MatPlotLib?

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)))
like image 478
SaiyanGirl Avatar asked Aug 20 '12 19:08

SaiyanGirl


People also ask

How do you plot an average plot in Python?

We define a window, calculate an average in the window, slide the window by one data point, and repeat until we get to the end. Here, we define a window size of 2 data points and use a list slice to get the subset of data we want to average. Then, we use NumPy to calculate the mean value.


2 Answers

I think this could be done most simply by doing 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

like image 57
ryanjdillon Avatar answered Sep 20 '22 05:09

ryanjdillon


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)
like image 45
BrenBarn Avatar answered Sep 18 '22 05:09

BrenBarn