0

I try to make a linear trendline (with a corresponding equation) with the following code, however, using what I have below creates a line that goes through each point, rather than creating a average. The function 'plt.plot(x1, y1,'r-')' worked in producing a linear trendline for a line graph I had constructed previously. Why would it not work for a scatter graph?

In addition to this, I was trying to eliminate the two -1 y-values, I have eliminated some x-values by defining x1 as shown bellow.

x = rainfall_container[0]
y = ndvi_container[0]

fig = plt.figure()

x1 = x[x > 0.0]
y1 = y[x > 0.0]

plt.scatter(x1, y1)
plt.plot(x1, y1,'r-')

enter image description here

Serenity
  • 35,289
  • 20
  • 120
  • 115
B.Grafton
  • 1
  • 2
  • 1
    what kind of trend are you looking to fit? Linear, polynomial? you need to start with a function of some kind, then try using scipy.optimize.curve_fit, then evaluate at the xvals and plot your data and plot the function evaluation. – Vince W. Jul 08 '16 at 03:25
  • 4
    Possible duplicate of [How to add trendline in python matplotlib dot (scatter) graphs?](http://stackoverflow.com/questions/26447191/how-to-add-trendline-in-python-matplotlib-dot-scatter-graphs) – wwii Jul 08 '16 at 03:33
  • Possible duplicate of [Python - calculating trendlines with errors](http://stackoverflow.com/questions/7171356/python-calculating-trendlines-with-errors) – moooeeeep Jul 08 '16 at 06:59
  • For the getting rid of y values that are less than 0 there is a simple error in your line `y1 = y[x > 0]`. You should change the `x` in this line to `y` – DavidG Jul 08 '16 at 08:19

0 Answers0