Piecewise linear distribution

from numpy import linspace import matplotlib.pyplot as plt def piecewise_linear_dist(x, bs, ps): ys = [] for i in range(len(bs)-1): b_diff = bs[i+1] - bs[i] if x < abs(b_diff): ys.append(ps[i] * ((bs[i+1] - x) / b_diff) + ps[i+1] * ((x - bs[i]) / b_diff)) return ys x = 2.5 points = 50 bs = linspace(300, 600, points) ps = linspace(30, 150, points) ys = piecewise_linear_dist(x, bs, ps) plt.plot(range(points-1), ys, 'k-') plt.tight_layout() plt.title("Piecewise linear distribution") plt.show() Piecewise linear distribution