Matplotlib中极值轮廓图上的插值差异 [英] Interpolation differences on polar contour plots in Matplotlib
问题描述
我试图在极坐标图上生成等高线图,并在matlab中进行了一些快速脚本编写以获得一些结果.出于好奇,我还想使用matplotlib在python中尝试相同的操作,但是不知何故,我看到了相同输入数据的不同轮廓图集.我试图弄清楚发生了什么,是否有什么可以在我的python代码中进行调整的方式,以在两种情况下均获得相似的结果.
I am trying to generate contour plots on a polar plot and did some quick scripting in matlab to get some results. Out of curiosity I also wanted to try out the same thing in python using the matplotlib but somehow I am seeing different sets of contour plots for the same input data. I am trying to figure out whats going on and if there is anything I could tweak in my python code to get similar results in both cases.
此处是matlab结果的屏幕截图:
A screenshot of the matlab results is here:
在Matlab代码中,我使用了scatteredinterpolant
函数来获取插值数据,我假设由于所使用的插值函数而出现了差异吗?
In the matlab code I used the scatteredinterpolant
function to get the interpolated data, I am assuming the differences are occurring due to the interpolation function used?
输入数据为-
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]
这是在spyder上使用python 2.7完成的.我已经尝试了scipy.interpolate.griddata
和matplotlib.mlab.griddata
,结果是相似的.我无法在mlab.griddata
中使用nn
方法,因为它一直为我提供被屏蔽的数据.
This was done using python 2.7, on spyder. I have tried both scipy.interpolate.griddata
as well as matplotlib.mlab.griddata
and the results are similar. I was unable to get the nn
method working in mlab.griddata
because it kept giving me masked data.
很抱歉,如果我错过任何相关内容-如果需要其他信息,请告诉我,我将更新我的帖子.
Apologies if I am missing anything relevant - please let me know if anyother info is required I will update my post.
线性Scipt网格数据图像如下所示:
The linear scipt griddata image looks like:
立方立体图像看起来像
And the cubic scipy image looks like
关于代码,这是代码-我将插值类型字符串传递到存在该代码的函数中.因此,线性"和三次"是2个输入.
As for the code, here is the code - I pass the interpolation type string into the function where this code is present. So 'linear' and 'cubic' are the 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)
角度输入是np.array(list(theta[i]))[x]
的结果-这是因为角度信息存储在元组列表中(这是因为我正在读取数据并对其进行排序).我看了一下代码,以确保数据正确并且似乎对齐. γ对应于半径,值是我提供的样本数据中的值.
希望这可以帮助!
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 in and sorting data). I took a look at the code to make sure the data is correct and it seems to line up. gamma corresponds to radii and values are the values in the sample data I provided.
Hope this helps!
推荐答案
matplotlib
中的极坐标图可能会比较棘手.发生这种情况时,一种快速的解决方案是将半径和角度转换为x,y,并以法线投影进行绘制.然后使一个空的极轴叠加在其上:
Polar plots in matplotlib
can get tricky. When that happens, a quick solution is to convert radii and angle to x,y, plot in a normal projection. Then make a 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])
这篇关于Matplotlib中极值轮廓图上的插值差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!