Polar color mesh from 1d arrays

51 Views Asked by At

I have three arrays

theta

array([180., 180., 180., 180., 180., 180., 180., 180., 180.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0., 225., 225., 225.,
       225., 225., 225., 225., 225., 225.,  45.,  45.,  45.,  45.,  45.,
        45.,  45.,  45.,  45.,  45., 270., 270., 270., 270., 270., 270.,
       270., 270., 270.,  90.,  90.,  90.,  90.,  90.,  90.,  90.,  90.,
        90.,  90., 315., 315., 315., 315., 315., 315., 315., 315., 315.,
       135., 135., 135., 135., 135., 135., 135., 135., 135., 135.])
phi

array([45.        , 39.98823529, 35.01176471, 30.        , 24.98823529,
       20.01176471, 15.        ,  9.98823529,  5.01176471,  0.        ,
        5.01176471,  9.98823529, 15.        , 20.01176471, 24.98823529,
       30.        , 35.01176471, 39.98823529, 45.        , 45.        ,
       39.98823529, 35.01176471, 30.        , 24.98823529, 20.01176471,
       15.        ,  9.98823529,  5.01176471,  0.        ,  5.01176471,
        9.98823529, 15.        , 20.01176471, 24.98823529, 30.        ,
       35.01176471, 39.98823529, 45.        , 45.        , 39.98823529,
       35.01176471, 30.        , 24.98823529, 20.01176471, 15.        ,
        9.98823529,  5.01176471,  0.        ,  5.01176471,  9.98823529,
       15.        , 20.01176471, 24.98823529, 30.        , 35.01176471,
       39.98823529, 45.        , 45.        , 39.98823529, 35.01176471,
       30.        , 24.98823529, 20.01176471, 15.        ,  9.98823529,
        5.01176471,  0.        ,  5.01176471,  9.98823529, 15.        ,
       20.01176471, 24.98823529, 30.        , 35.01176471, 39.98823529,
       45.        ])
violet_ints

array([ 60821.47285091,  71469.47778826,  81950.58139812,  92660.95741024,
       103228.07719577, 113446.6171393 , 123862.48451513, 132666.65201113,
       138673.94497866, 141418.87424494, 140947.68057807, 137704.80141046,
       131712.10023037, 122871.74227686, 111022.93897237,  98051.87876927,
        85884.71515151,  74789.81179595,  64223.21535332,  47239.6029958 ,
        59060.46569283,  71631.51992138,  85056.88727878,  97838.32318773,
       110985.04206186, 123046.60805235, 131994.74029036, 138009.81758468,
       141092.29699058, 141436.99265739, 139367.40863866, 134564.53259257,
       127256.22737589, 117476.56579378, 105893.78184852,  94434.79559289,
        83012.77977437,  71513.83906988,  35465.27768276,  52489.59008641,
        70259.4064192 ,  87011.24343702, 101845.51856053, 114496.37908394,
       125113.38041456, 132956.15831876, 138245.26502938, 140988.36471711,
       141467.04309028, 139302.73813352, 135346.73647267, 128688.98045284,
       120273.38645452, 110273.45442647,  99177.33744858,  87341.78739578,
        74874.97094334,  50986.33091372,  63371.63698099,  76937.62936534,
        91341.43206481, 105418.35862792, 117371.0063522 , 127397.07283259,
       134693.40421132, 139258.98685025, 141392.73492377, 140797.81487188,
       137360.21016255, 130967.39590833, 122385.40640793, 113290.91743172,
       103305.45203984,  93133.15852394,  82350.70121816,  71046.51493323])

I made a polar plot with these arrays

plt.figure(figsize=(10, 20))
ax = plt.axes(projection='polar')
axx = ax.scatter(theta*np.pi/180, phi, c=violet_ints,
                                       cmap='viridis')
plt.colorbar(axx, shrink = 0.4);

Polar plot

Now I want to interpolate these data so that I can get a color mesh plot like this one.

Interpolated plot:

Interpolated plot

I tried to convert my arrays into meshgrids

Theta, Phi = np.meshgrid(theta, phi)
_, Violet_Ints = np.meshgrid(violet_ints, violet_ints)

However when I try to make a plot using pcolormesh, it does not make sense

plt.figure(figsize=(10, 20))
ax = plt.axes(projection='polar')

ax.pcolormesh(Theta*np.pi/180, Phi, Violet_Ints,
                                       cmap='viridis');

My attempt to plot meshgrid:

My attempt to plot meshgrid

Can you guys help me with this one, please?

1

There are 1 best solutions below

1
Cameron Riddell On

The main thing you're missing is that pcolormesh attempts to interpolate between adjacent values. This means that the sortedness of your data is important. Once you fix that, you'll need to do a little numpy to ensure you satisfy the coordinate system that .pcolormesh needs.

Using your variables from above:

import matplotlib.pyplot as plt
import numpy as np



theta = np.radians(theta)
idx = np.argsort(theta)
theta, phi, violet_ints = theta[idx], phi[idx], violet_ints[idx] # reroder data according to theta
theta_g, phi_g = np.meshgrid(theta, phi)                         # coordinates of theta/pho
_, violet_g = np.meshgrid(violet_ints, violet_ints)              # tile violet to align on coordinate grid
theta_g = np.hstack([theta_g, theta_g[:, [0]]])                  # append a column that is same as the first
phi_g   = np.hstack([phi_g,     phi_g[:, [0]]])

mosaic = [
    ['scatter', '.'      ],
    ['flat',    'gouraud'],
]
fig, axd = plt.subplot_mosaic(
    mosaic, figsize=(15, 15), subplot_kw={'projection': 'polar'}, gridspec_kw={'hspace': .5}
)

# plot scatter
axd['scatter'].scatter(theta, phi, c=violet_ints, cmap='viridis')
axd['scatter'].set_title('Scatter', size='xx-large', loc='left')

# plot "Flat Shading" pcolormesh
violet_g_flat = violet_g[:-1, :]
axd['flat'].pcolormesh(theta_g, phi_g, violet_g_flat, cmap='viridis', shading='flat');
axd['flat'].set_title('Flat Shading', loc='left', size='xx-large')

# plot "Gouraud Shading" pcolormesh
#   note that we use the same trick to "wrap" out colors
violet_g_gouraud = np.hstack([violet_g, violet_g[:, [0]]])
axd['gouraud'].pcolormesh(theta_g, phi_g, violet_g_gouraud, cmap='viridis', shading='gouraud');
axd['gouraud'].set_title('Gouraud Shading', loc='left', size='xx-large')

plt.show()

enter image description here