Skip to content

Instantly share code, notes, and snippets.

@misho104
Forked from ev-br/gist:9081651
Last active February 17, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misho104/46032fa730088a0cb4c2e0556c59260b to your computer and use it in GitHub Desktop.
Save misho104/46032fa730088a0cb4c2e0556c59260b to your computer and use it in GitHub Desktop.
akima / pchip
from scipy.interpolate import Akima1DInterpolator as Akima
from scipy.interpolate import PchipInterpolator as pchip
from scipy.interpolate import CubicSpline as cubic
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
np.random.seed()
def show_plot(x, y):
p = pchip(x, y)
c = cubic(x, y, bc_type='natural')
c2 = cubic(x, y)
a = Akima(x, y)
xp = np.sort(x[0] + (x[-1] - x[0])*np.random.random(200))
plt.plot(x, y, 'b+', ms=8, mec='r')
plt.plot(xp, c(xp), 'y-', lw=3, alpha=0.7) # yellow for Cubic/natural
plt.plot(xp, c2(xp), 'y-', lw=3, alpha=0.3) # light-yellow for Cubic/not-a-knot
plt.plot(xp, a(xp), 'b-', lw=2, alpha=0.5) # light-blue for Akima
plt.plot(xp, p(xp), 'm-', lw=1, alpha=1) # red for pchip
plt.draw()
x = range(20)
y = 10 * np.sort(np.random.random(20))
show_plot(x, y)
x = range(20)
y = 10 * np.random.random(20)
show_plot(x, y)
x = 10 * np.sort(np.random.random(20))
y = 10 * np.sort(np.random.random(20))
show_plot(x, y)
x = 10 * np.sort(np.random.random(20))
y = 10 * np.random.random(20)
show_plot(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment