Search code examples
pythonmatplotlibpolar-coordinates

Polar pcolormesh plot shows offset (how to display two arrays in different hemispheres of a polar plot?)


How can I (in python) accurately and flexibly display two arrays in different hemispheres of a polar plot?

I would like to plot an array such that half of it appears on the top hemisphere of a polar plot, and half appears on the bottom. (I am splitting the array into two halves so that I can use two different colormaps: open to different ways of doing this as long as that functionality is retained.) I am currently displaying the array with pcolormesh, but cannot seem to define the grid correctly to avoid having an offset in the plot. A simple example with mock data follows. I would like this example to show a single straight line from the top of the plot to the bottom -- red on top, blue on the bottom, with no gap in the middle.

# make test data: should appear as a straight line down the center
Rtest = np.zeros((39, 165), np.float_)
Rtest[:, 82] = 1.
Itest = np.ones(39)

fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")

# plot one half on the bottom
nv1, nthets1 = Rtest[:20, :].shape
r1 = np.linspace(0, nv1, nv1) 
t1 = np.linspace(np.pi, np.pi*2, nthets1+1, endpoint=True)
R1, T1  = np.meshgrid(r1, t1)
ax.pcolormesh(T1, R1, Rtest[:20, :].T, cmap='Blues')

# plot the other half on top
nv2, nthets2 = Rtest[20:, :].shape
r2 = np.linspace(0, nv2, nv2)
t2 = np.linspace(0, np.pi, nthets2, endpoint=False)
R2,T2  = np.meshgrid(r2, t2)
ax.pcolormesh(T2, R2, Rtest[20:, :].T, cmap='Reds')#, **newallkwargs)

ax.plot(0, 0, '.')

Instead, the following is what is displayed. (I have also plotted a dot at (0, 0) to emphasize the offset I am seeing.) There is a radial gap in the center that I would like to avoid, as well as what appears to be an offset in theta between the two halves.

The mock data reflect the general case: the array lengths can be odd, such that the array can't be cut into two even halves. In that case I would like the shorter half to simply not extend as far radially as the other (as is happening in the red semicircle below).

What is displayed by the MWE.

Any pointers on polar pcolormesh plots, or on how to achieve the desired effect, are greatly appreciated.


Solution

  • The "line" seems cut, because the resolution of the image isn't high enough. One pixel of the pcolor image is much smaller than one pixel in the png image. This is a common problem with downsampling data, and there isn't any canonical solution to it, appart from not downsampling too much of course.

    In this case, already a dpi of 100 is enough to show the line though, plt.figure(dpi=100)

    enter image description here