Sing Sing
Sing Sing

Reputation: 41

Plot average of an array in python

I have a 2D array of temperature over time data. There are about 7500 x-values and as much corresponding y-values (so one y for every x).

It looks like this: plot

The blue line in the middle is the result of my unsuccessful attempt to draw a plot line, which would represent the average of my data. Code:

import numpy as np
import matplotlib.pyplot as plt
data=np.genfromtxt("data.csv")
temp_av=[np.mean(data[1])]*len(data[0])
plt.figure()
plt.subplot(111)
plt.scatter(data[0],data[1])
plt.plot(data[0],temp_av)
plt.show()

However what I need is a curve, which will follow the rise in the temperature. Basically a line which will be somewhere in the middle of data points.

I googled for some solutions, but all I found were suggestions how to compute an average in cases where you have multiple y-values for one x. I understand how to do that, but it doesn't help in this case.

My next idea would be to use a loop to compute an average for every 2 neighbor points. But I am not sure how to do that best and if there aren't better solutions.

Also, I understand that what I need is to compute an other array. Plotting is only for representation.

Upvotes: 4

Views: 9987

Answers (1)

Jorge Torres
Jorge Torres

Reputation: 1466

If I undestrand correclty, what you are trying to plot is a trend line. You could do it by using the numpy function 'polyfit'. If that's what you are looking for, try this small modification to your code

import numpy as np
import matplotlib.pyplot as plt
data=np.genfromtxt("data.csv")
plt.figure()
plt.subplot(111)
plt.scatter(data[0],data[1])

pfit = np.polyfit(data[0], data[1], 1)
trend_line_model = np.poly1d(pfit)

plt.plot(data[0], trend_line_model(data[0]), "m--") 
plt.show()

This will plot the trend line in dashed magenta

Upvotes: 1

Related Questions