更新:我已经完成了我在
http://blog.rtwilson.com/producing-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:输入x和y必须是1D或2D ..我不知道为什么我得到这一点,因为x和y都是2D.我做错了吗?
此外,将我的模型返回的值放入列表中,然后重塑它似乎相当笨重.有没有更好的方法来做到这一点?