I have small sets of irregularly-spaced data taken at various points over a circular area, in polar format. I need to do interpolation to get the data on a regularly-spaced grid, and then I'd like to plot them using a contour plot.
I've managed to do the interpolation and plot the result, but I have to convert from polar to rectangular coordinates to do the interpolation, and I get artifacts on the polar plot when I convert the data back to polar coordinates.
The following code demonstrates what I have so far, and plots the data on a polar and rectangular plot:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
# inputs as 1D arrays
r = np.array([0, 1, 1, 1, 1, 2, 2, 2, 2])
theta = np.radians(np.array([0, 90, 180, 270, 0, 90, 180, 270, 0]))
# z = f(theta, r)
z = np.array([8, 7, 6, 4, 5, 2, 2, 2, 2])
# convert to rect
x = r * np.cos(theta)
y = r * np.sin(theta)
# create RBF for smoothing
rbf = Rbf(x, y, z)
# create grid to smooth over
xi, yi = np.mgrid[-2:2:10j, -2:2:10j]
# smooth
zi = rbf(xi, yi)
# convert back to polar
ri = np.sqrt(xi*xi + yi*yi)
ti = np.arctan2(yi, xi)
# polar plot
fig = plt.figure()
ax = plt.subplot(121, polar=True)
cax = ax.contour(ti, ri, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(ti, ri, zi, 10, cmap=plt.cm.Spectral)
ax.set_rmax(2)
# rect plot
ax = plt.subplot(122)
cax = ax.contour(xi, yi, zi, 10, linewidths=0.5, colors='k')
cax = ax.contourf(xi, yi, zi, 10, cmap=plt.cm.Spectral)
plt.show()
The remaining issues are:
- Can I fix the contour line artifacts?
- Does Scipy provide a more appropriate interpolation algorithm that works for such small datasets containing polar coordinates?
