matplotlib combine polar and cartesian gridded data

假装没事ソ 提交于 2019-12-07 06:11:06

问题


How do I combine gridded polar and Cartesian data on a plot?

It is important to note that axes origins and scales have to match.

In my application, I want to combine weather radar data ( polar ) with elevation data (Cartesian).

This is the starting point :


回答1:


Thinking about this for a while, the answer is you need to translate you radial data into Cartesian space:

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')




回答2:


See this other answer for more information and explanations.
Basically, you can create two overlapping axes-objects. Here is a minimal working example (which looks terrible, but illustrates the point):

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()

The trick is to have both axis in the same location and for the second choosing frameon=false. Your figure will look like this:



来源:https://stackoverflow.com/questions/18789157/matplotlib-combine-polar-and-cartesian-gridded-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!