繁体   English   中英

matplotlib 中的极地等高线图 - 最好的(现代)方法吗?

[英]Polar contour plot in matplotlib - best (modern) way to do it?

更新:我已经在我的博客http://blog.rtwilson.com/production-polar-contour-plots-with-matplotlib/上完整地记录了我发现这样做的方式 - 你可能想要首先检查那里。

我正在尝试在 matplotlib 中绘制极地等高线图。 我在互联网上找到了各种资源,(a) 我似乎无法让我的代码工作,(b) 许多资源看起来很旧,我想知道现在是否有更好的方法。 例如, http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg01953.html表明可能会采取一些措施来尽快改善情况,那是在 2006 年!

我很想能够绘制正确的极地等高线图 - 就像 pcolor 可以让您针对其类型的图进行操作(请参阅下面的注释部分),但我似乎找不到任何方法来做到这一点,所以我首先转换为笛卡尔坐标。

无论如何,我有以下代码:

from pylab import *
import numpy as np

azimuths = np.arange(0, 360, 10)
zeniths = np.arange(0, 70, 10)
values = []

for azimuth in azimuths:
  for zenith in zeniths:
    print "%i %i" % (azimuth, zenith)
    # Run some sort of model and get some output
    # We'll just use rand for this example
    values.append(rand())

theta = np.radians(azimuths)

values = np.array(values)
values = values.reshape(len(zeniths), len(azimuths))

# This (from http://old.nabble.com/2D-polar-surface-plot-td28896848.html)
# works fine
##############
# Create a polar axes
# ax = subplot(111, projection='polar')
# pcolor plot onto it
# c = ax.pcolor(theta, zeniths, values)
# show()

r, t = np.meshgrid(zeniths, azimuths)

x = r*np.cos(t)
y = r*np.sin(t)

contour(x, y, values)

当我运行时,我收到一个错误TypeError: Inputs x and y must be 1D or 2D. . 我不确定为什么会得到这个,因为 x 和 y 都是二维的。 难道我做错了什么?

此外,将我的模型返回的值放入列表然后重新调整它似乎相当笨拙。 有一个更好的方法吗?

您应该能够像往常一样将ax.contourax.contourf与极坐标图一起使用……不过,您的代码中有一些错误。 您将事物转换为弧度,然后在绘图时使用以度为单位的值。 此外,当它期望theta, r时,您将r, theta传递给轮廓。

举个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)

r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)

plt.show()

在此处输入图片说明

x、y 和值的形状必须相同。 您的数据形状是:

>>> x.shape, y.shape, values.shape
((36, 7), (36, 7), (7, 36))

因此将轮廓(x,y,值)更改为轮廓(x,y,values.T)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM