Interpolation differences on polar contour plots in Matplotlib

I am trying to create contour plots in the polar region and made some quick scripts in Matlab to get some results. Out of curiosity, I also wanted to try the same thing in python using matplotlib, but somehow I see different sets of contour plots for the same input. I am trying to figure out what is happening, and if there is something that I could change in my Python code to get similar results in both cases.

A screenshot of the Matlab results is here: enter image description here

In matlab code, I used the scatteredinterpolant function to get the interpolated data, I assume the differences are due to the interpolation function used?

Input data -

 Angles = [-180, -90, 0 , 90, 180, -135, -45,45, 135, 180,-90, 0, 90, 180 ] Radii = [0,0.33,0.33,0.33,0.33,0.5,0.5,0.5,0.5,0.5,0.6,0.6,0.6,0.6] Values = [30.42,24.75, 32.23, 34.26, 26.31, 20.58, 23.38, 34.15,27.21, 22.609, 16.013, 22.75, 27.062, 18.27] 

This was done using python 2.7, on spyder. I tried both scipy.interpolate.griddata and matplotlib.mlab.griddata , and the results are similar. I was not able to get the nn method working in mlab.griddata because it continued to give me masked data.

I apologize if I am missing something meaningful - let me know if any information is needed. I will update my post.

Edit:

The griddata scipt line image looks like this: enter image description here

And the cubic scipy image looks enter image description here

As for the code, here is the code - I pass the string like interpolation to the function where this code is present. Thus, β€œlinear” and β€œcubic” are 2 inputs.

 val = np.array(list(values[i])) radius = np.array(list(gamma[i])) ang = [math.radians(np.array(list(theta[i]))[x]) for x in xrange(0,len(theta[i]))] radiiGrid = np.linspace(min(radius),max(radius),100) anglesGrid = np.linspace(min(ang),max(ang),100) radiiGrid, anglesGrid = np.meshgrid(radiiGrid, anglesGrid) zgrid = griddata((ang,radius),val,(anglesGrid,radiiGrid), method=interpType) 

The angle input is what comes out of np.array(list(theta[i]))[x] - this is because the angle information is stored in a list of tuples (this is because I am reading and sorting the data). I took a look at the code to make sure the data is correct and seems to line up. the gamma corresponds to the radii, and the values ​​are the values ​​in the sample data that I provided. Hope this helps!

+4
source share
1 answer

Polar plots in matplotlib can be tricky. When this happens, a quick solution is to convert the radii and angles to x, y, a graph in normal projection. Then make an empty polar axis to superimpose on it:

 from scipy.interpolate import griddata Angles = [-180, -90, 0 , 90, 180, -135, -45,45, 135, 180,-90, 0, 90, 180 ] Radii = [0,0.33,0.33,0.33,0.33,0.5,0.5, 0.5,0.5,0.5,0.6,0.6,0.6,0.6] Angles = np.array(Angles)/180.*np.pi x = np.array(Radii)*np.sin(Angles) y = np.array(Radii)*np.cos(Angles) Values = [30.42,24.75, 32.23, 34.26, 26.31, 20.58, 23.38, 34.15,27.21, 22.609, 16.013, 22.75, 27.062, 18.27] Xi = np.linspace(-1,1,100) Yi = np.linspace(-1,1,100) #make the axes f = plt.figure() left, bottom, width, height= [0,0, 1, 0.7] ax = plt.axes([left, bottom, width, height]) pax = plt.axes([left, bottom, width, height], projection='polar', axisbg='none') cax = plt.axes([0.8, 0, 0.05, 1]) ax.set_aspect(1) ax.axis('Off') # grid the data. Vi = griddata((x, y), Values, (Xi[None,:], Yi[:,None]), method='cubic') cf = ax.contour(Xi,Yi,Vi, 15, cmap=plt.cm.jet) #make a custom colorbar, because the default is ugly gradient = np.linspace(1, 0, 256) gradient = np.vstack((gradient, gradient)) cax.xaxis.set_major_locator(plt.NullLocator()) cax.yaxis.tick_right() cax.imshow(gradient.T, aspect='auto', cmap=plt.cm.jet) cax.set_yticks(np.linspace(0,256,len(cf1.get_array()))) cax.set_yticklabels(map(str, cf.get_array())[::-1]) 

enter image description here

+2
source

All Articles