Matplotlib combines polar and cartesian gridded data

How to combine grid polar and cartesian data on a graph?

It is important to note that the origin of the axes and the scales must be the same.

In my application I want to combine weather radar data (polarity) with altitude data (Cartesian).

This is the starting point:

enter image description here

+4
source share
2 answers

Thinking about this for a while, the answer is you need to translate the 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') 

output

+2
source

See this other answer for more information and explanations .
Basically, you can create two overlapping object axes. Here is a minimal working example (which looks awful 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 axes in the same place, and in the second choice frameon=false . Your figure will look like this:

enter image description here

+6
source

Source: https://habr.com/ru/post/1502071/


All Articles