【问题标题】:matplotlib combine polar and cartesian gridded datamatplotlib 结合极坐标和笛卡尔网格数据
【发布时间】:2013-09-13 14:44:33
【问题描述】:

如何在绘图上组合网格化的极坐标和笛卡尔数据?

重要的是要注意轴的原点和比例必须匹配。

在我的应用程序中,我想将天气雷达数据(极坐标)与高程数据(笛卡尔坐标)结合起来。

这是起点:

【问题讨论】:

标签: python matplotlib layer polar-coordinates


【解决方案1】:

See this other answer for more information and explanations.
基本上,您可以创建两个重叠的轴对象。这是一个最小的工作示例(看起来很糟糕,但说明了这一点):

import numpy as np
import matplotlib.pyplot as plt

# setting up data
line = np.random.rand(5)
r = np.arange(0, 3.0, 0.01)
theta = 2 * np.pi * r

# initializing the figure
fig = plt.figure()
# setting the axis limits in [left, bottom, width, height]
rect = [0.1, 0.1, 0.8, 0.8]

# the carthesian axis:
ax_carthesian  = fig.add_axes(rect)
# the polar axis:
ax_polar = fig.add_axes(rect, polar=True, frameon=False)

# plotting the line on the carthesian axis
ax_carthesian.plot(line,'b')

# the polar plot
ax_polar.plot(theta, r, color='r', linewidth=3)
ax_polar.set_rmax(2.0)
ax_polar.grid(True)

plt.show()

诀窍是让两个轴在同一个位置,第二个选择frameon=false。你的图会是这样的:

【讨论】:

  • 这是一个很好的起点,但原点和比例必须匹配。
  • @egouden 您现在看到了吗,您的问题缺少重要信息?有时间我会在编辑中解决这个问题。
  • 我的问题的当前版本还可以。我在做物理。很明显,我想将两个数据集与相应的位置进行比较。我仍然愿意讨论:请提供您不想要此结果的任何其他应用程序。
  • @egouden 有些事情对你来说可能很明显,但对其他人来说可能不是。轴是任意选择。当然,有些比其他的更有用,但是极坐标网格和笛卡尔网格可以通过多种方式对齐。并且 - 不一定需要比例匹配。这在很大程度上取决于您的数据或您要显示的内容。
  • 很好。你觉得我的问题现在可以了吗?如果是这样,请删除您的 -1 投票以及指向先前回答的问题的链接(不一样,而且 IMO 的趣味性要低得多)。那么大师可能有兴趣解决实际问题。
【解决方案2】:

想了一会儿,答案是您需要将径向数据转换为笛卡尔空间:

import copy
# fake....err, simulated... data

# elevation
X, Y = np.meshgrid(np.linspace(-50, 50, 1024), np.linspace(-50, 50, 1024))
elv = np.sin(np.pi * X / 50) * np.cos(2*np.pi * Y / 50)

# radar
R, theta = np.meshgrid(np.linspace(0, 35, 512), np.linspace(0, 2*np.pi, 512))
rad = np.sin(3*theta) ** 2 * np.cos(R / 10) ** 2

Rt_x = R * np.sin(theta)  # turn radial grid points into (x, y)
Rt_y = R * np.cos(theta)

fig, ax = plt.subplots(1, 1)
ax.set_aspect('equal')
# plot contour
ax.contour(X, Y, elv, cmap='gray')

# tweak color map so values below a threshold are transparent
my_cmap = copy.copy(cm.get_cmap('jet'))
my_cmap.set_under(alpha=0)
# plot the radar data
ax.pcolormesh(Rt_x, Rt_y, rad, zorder=5, edgecolor='face', cmap=my_cmap, vmin=.5, shading='flat')

【讨论】:

  • 这是一个很好的答案。我仍然不明白 pcolormesh 如何知道在哪里快速着色。但是,您是否设法获得适当的矢量输出?对我来说,保存到 pdf 需要太多时间,并且使用 pcolorfast 和极轴会给我一个空白图像。那么渲染几乎是不可能的。
  • @egouden 您的原始帖子没有提到矢量输出,我的回答既不是pcolorfast,也不是极地exes。请用你的新问题提出一个新问题。将事物渲染为 png 非常快。
  • 很公平。让我们一步一步来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多