【问题标题】:Interpolation differences on polar contour plots in MatplotlibMatplotlib 中极地等高线图的插值差异
【发布时间】:2015-08-13 14:25:55
【问题描述】:

我正在尝试在极坐标图上生成等高线图,并在 matlab 中快速编写脚本以获得一些结果。出于好奇,我也想在 python 中使用 matplotlib 尝试同样的事情,但不知何故,我看到相同输入数据的不同等高线图集。我试图弄清楚发生了什么,如果有什么我可以在我的 python 代码中调整以在两种情况下获得相似的结果。

matlab 结果的截图如下:

在 matlab 代码中,我使用scatteredinterpolant 函数来获取插值数据,我假设由于使用了插值函数而出现差异?

输入数据是-

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.griddatamatplotlib.mlab.griddata,结果相似。我无法让nn 方法在mlab.griddata 中工作,因为它一直给我屏蔽数据。

抱歉,如果我遗漏了任何相关信息 - 如果需要任何其他信息,请告诉我,我会更新我的帖子。

编辑:

线性 scipt griddata 图像如下所示:

立方 scipy 图像看起来像

至于代码,这里是代码 - 我将插值类型字符串传递给存在此代码的函数。所以 'linear' 和 'cubic' 是 2 个输入。

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] - 这是因为角度信息存储在元组列表中(这是因为我正在读取和排序数据)。我查看了代码以确保数据正确并且似乎对齐。伽玛对应于半径,值是我提供的样本数据中的值。 希望这可以帮助!

【问题讨论】:

  • 能否提供python轮廓的图像,让我们知道这两种方法有何不同?
  • 什么 matplotlib 代码和生成了什么图像?我们需要看到您看到的差异,而不仅仅是一张图片。
  • 用相关信息更新了原帖。我不确定我是如何设法不包括其他情节的,因为它们都在同一个 imgur 链接中。但现在这些都更新了。
  • @tom 感谢您修复帖子。我不知道如何做内联图像..下次会记住它。
  • 这只是需要更多代表的问题 :)

标签: python matlab matplotlib plot


【解决方案1】:

matplotlib 中的极坐标图可能会变得棘手。发生这种情况时,一个快速的解决方案是将半径和角度转换为 x,y,在法线投影中绘制。然后做一个空的极轴叠加在上面:

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])

【讨论】:

  • 这太棒了。我有效地知道这也是 MATLAB 所做的。我不确定如何在这里复制它。这看起来比我已经拥有的要好得多。谢谢!
  • 快速问题 - 如果无论如何都要关闭,为什么要设置 ax.set_aspect(1)?
  • ax.axis('Off') 关闭显示(x、y、网格、框等),ax 本身不会消失。您可以留下它'on' 以确保叠加的极坐标图对齐良好。然后关掉它,否则它看起来很忙。
  • 这几天我要对Maplotlib 中的极坐标绘图方法进行一次大清理。也许在我辞职或被裁员之后。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多