2

For example, in matplotlib, I plot a simple curve based on few points:

from matplotlib import pyplot as plt
import numpy as np

x=[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
       2.6, 2.7, 2.8, 2.9]
y=[0.0, 0.19, 0.36, 0.51, 0.64, 0.75, 0.8400000000000001, 0.91, 0.96, 0.99, 1.0, 
   0.99, 0.96, 0.9099999999999999, 0.8399999999999999, 0.75, 0.6399999999999997, 
   0.5099999999999998, 0.3599999999999999, 0.18999999999999995, 0.0, 
   -0.20999999999999996, -0.4400000000000004, -0.6900000000000004, 
   -0.9600000000000009, -1.25, -1.5600000000000005, -1.8900000000000006, 
   -2.240000000000001, -2.610000000000001]

plt.plot(x,y)
plt.show()

Hypothetically, say I want to highlight the point on the curve where the x value is 0.25, but I don't know the y value for this point. What should I do?

2 Answers 2

3

The easiest solution is to perform a linear interpolation between neighboring points for the provided x value. Here is a sample code to show the general principle:

X=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2,
   1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
   2.6, 2.7, 2.8, 2.9]
Y=[0.0, 0.19, 0.36, 0.51, 0.64, 0.75, 0.8400000000000001, 0.91, 0.96,
   0.99, 1.0, 0.99, 0.96, 0.9099999999999999, 0.8399999999999999, 0.75,
   0.6399999999999997, 0.5099999999999998, 0.3599999999999999,
   0.18999999999999995, 0.0, -0.20999999999999996, -0.4400000000000004,
   -0.6900000000000004, -0.9600000000000009, -1.25, -1.5600000000000005,
   -1.8900000000000006, -2.240000000000001, -2.610000000000001]

def interpolate(X, Y, xval):
    for n, x in enumerate(X):
        if x > xval: break
    else: return None # xval > last x value
    if n == 0: return None # xval < first x value
    xa, xb = X[n-1], X[n] # get surrounding x values
    ya, yb = Y[n-1], Y[n] # get surrounding y values
    if xb == xa: return ya # 
    return ya + (xval - xa) * (yb - ya) / (xb - xa) # compute yval by interpolation

print(interpolate(X, Y, 0.25)) # --> 0.435 
print(interpolate(X, Y, 0.85)) # --> 0.975
print(interpolate(X, Y, 2.15)) # --> -0.3259999999999997
print(interpolate(X, Y, -1.0)) # --> None (out of bounds)
print(interpolate(X, Y, 3.33)) # --> None (out of bounds)

Note: When the provided xval is not within the range of x values, the function returns None

Sign up to request clarification or add additional context in comments.

Comments

0

You could manually do linearly interpolation like this:

def get_y_val(p):
    lower_i = max(i for (i, v) in enumerate(x) if v<= p)
    upper_i = min(i for (i, v) in enumerate(x) if v>= p)
    d = x[upper_i] - x[lower_i]
    if d == 0:
        return y[lower_i]
    y_pt = y[lower_i] * (x[upper_i] - p) / d+ y[upper_i] * (p - 
    x[lower_i]) / d
    return y_pt

1 Comment

good point. I did not realize by default pyplot uses linear interpolation, the curve looks very smooth to me. but as I tried again using fewer and more sparsely located points, I see it is linear interpolation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.