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.
and
However, what I want is this (an average of all the points along the way):
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)))
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.
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:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With