Reputation: 6217
Given a Line2D
object, which is the output of pyplot
's plot
function, I wish to determine the y
coordinate of a given x
. For example, if
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1.0, 4.0, 9.0, 16.0])
y = np.array([1.0, 2.0, 3.0, 4.0])
line = plt.plot(x, y)[0]
then
def get_y(line, x):
...
## This should print something close to 2.0
print get_y(line, 2) ** 2
I tried using path = line.get_path()
, and then interpolate but things haven't worked as I have expected. I thought there must be a standard way of doing that...
Upvotes: 1
Views: 3801
Reputation: 1
Maybe numpy.interp(x, xp, fp, left=None, right=None)
can help you
http://docs.scipy.org/doc/numpy/reference/generated/numpy.interp.html
Upvotes: 0
Reputation: 21
If i understand correctly you want to know the height y
of a point p
on a line ab
.
Assuming that the line is infinite we can do it this way.
Calculate the line slope:
height = lineEndY - lineStartY #calculate the line "height"
lenghtX = abs(lineStartX - lineEndX) #calculate the length of line along the X axis
slope = height / lengthX; #calculate the "slope" by deviding length of x by height
result = x * slope; #calculate the x approx height on the line
Upvotes: 1