问题描述
更新:我已经在 http://blog.rtwilson.com/production-polar-contour-plots-with-matplotlib/-您可能要检查一下首先.
Update: I've done a full write-up of the way I found to do this on my blog at http://blog.rtwilson.com/producing-polar-contour-plots-with-matplotlib/ - you may want to check there first.
我正在尝试在matplotlib中绘制极坐标轮廓图.我在Internet上发现了各种资源,(a)我的代码似乎无法正常工作,并且(b)许多资源显得很陈旧,我想知道现在是否有更好的方法.例如, http://www.mail-archive. com/[email protected]/msg01953.html 建议尽快采取措施改善现状,那是在2006年!
I'm trying to plot a polar contour plot in matplotlib. I've found various resources on the internet, (a) I can't seem to get my code to work and (b) many of the resources appear rather old, and I'm wondering if there is a better way now. For example, http://www.mail-archive.com/[email protected]/msg01953.html suggests that something may be done to improve things soon, and that was in 2006!
我希望能够绘制正确的极坐标轮廓图-就像pcolor可以让您对其图类型进行绘制(请参阅下面的注释部分),但是我似乎找不到任何方法,所以我首先要转换为笛卡尔坐标.
I'd love to be able to plot proper polar contour plots - like pcolor lets you do for its type of plot (see commented out section below), but I can't seem to find any way to do that, so I'm converting to cartesian co-ordinates first.
无论如何,我有以下代码:
Anyway, I have the code that follows:
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都是2D的.我在做错什么吗?
When I run that I get an error TypeError: Inputs x and y must be 1D or 2D.
. I'm not sure why I get this, as both x and y are 2D. Am I doing something wrong?
此外,将我从模型返回的值放在列表中然后重塑它似乎很笨拙.有更好的方法吗?
Also, it seems rather clunky to be putting my values returned from my model into a list and then reshaping it. Is there a better way to do this?
推荐答案
您应该能够像平常一样将ax.contour
或ax.contourf
用于极坐标图...您的代码中有一些错误, 尽管.您将事物转换为弧度,但是在绘制时使用度数.另外,当需要theta, r
时,您正在传递r, theta
轮廓.
You should just be able to use ax.contour
or ax.contourf
with polar plots just as you normally would... You have a few bugs in your code, though. You convert things to radians, but then use the values in degrees when you plot. Also, you're passing in r, theta
to contour when it expects theta, r
.
作为一个简单的例子:
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()
这篇关于matplotlib中的极坐标轮廓图-最佳(现代)方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!