**更新:**我已经在我的博客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: Inputs x and y must be 1D or 2D.
。我不知道为什么会这样,因为x和y都是二维的。我做错什么了吗?
此外,将从模型返回的值放入列表中,然后重新塑造它似乎相当笨拙。有没有更好的办法?
2条答案
按热度按时间cngwdvgl1#
您应该可以像通常一样使用
ax.contour
或ax.contourf
来绘制极坐标图。你的代码里有一些bug。您可以将物体转换为弧度,但在打印时使用以度为单位的值。此外,当contour需要theta, r
时,您将r, theta
传递给它。举个简单的例子:
jmp7cifd2#
x、y和值的形状必须相同。您的数据形状为:
所以将contour(x,y,values)改为contour(x,y,values.T)。