Issue
I have a polar plot that uses pcolormesh to plot a histogram in a 'ring' between two radial values. I have the code working for flat shading, but would like to use gouraud, but this gives error (for the example below):
Dimensions of C (1, 10) are incompatible with X (11) and/or Y (2); see help(pcolormesh)
If I use:
pcm = ax.pcolormesh(theta_mesh[:-1,:-1]*np.pi/180, r_mesh[:-1,:-1], hist, cmap=cmap, shading='gouraud')
then I have no error, but also nothing is visible in the plot.
Here is an example with data:
import numpy as np
import matplotlib.pyplot as plt
from numpy.ma import masked_array
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}, figsize = (6,6))
ax.set_theta_zero_location('S')
ax.set_theta_direction(1)
theta_ticks = np.array([10, 0, -10, -20, -30, -40])
theta_labels = [f'{t}' for t in theta_ticks]
ax.set_xticks(np.radians(theta_ticks))
ax.set_xticklabels(theta_labels)
r_ticks = np.linspace(1, 0.1, 10)
ax.set_yticks(r_ticks)
ax.set_rlim(1,0.1)
ax.set_axisbelow(True)
ax.grid(alpha=0.4)
ax.set_thetamin(-40)
ax.set_thetamax(10)
n_rings = 7
n_segments = 15
cmap = plt.get_cmap('rainbow')
r = np.array([0.27718767, 0.26992833, 0.3291085, 0.219035, 0.3291085, 0.32943633,
0.30544525, 0.222413, 0.27754, 0.27278267, 0.286725, 0.219035,
0.22321733, 0.30544525, 0.219035, 0.3291085, 0.271587, 0.328375,
0.32943633, 0.30726033, 0.30726033, 0.33995833, 0.23273433, 0.27318767,
0.27718767, 0.28464367, 0.3291085, 0.219035, 0.34695067, 0.271587,
0.218609, 0.26349133, 0.309604, 0.20733267, 0.27786567, 0.309604,
0.21290633, 0.27786567, 0.3291085, 0.271587, 0.3291085, 0.27754,
0.324375, 0.23273433, 0.328375, 0.26992833, 0.32943633, 0.219035,
0.27754, 0.322597, 0.27672, 0.339284 ])
th = np.array([ -6.19994195, -18.41777188, -9.26144056, -12.03341037, -8.558630,
-8.95986186, -7.35883334, -15.33784359, -21.03901875, -17.43569267,
-7.51616751, -15.88593498, -14.20710742, -12.72758571, -13.4987869,
-8.51990186, -14.85967987, -7.99177069, -9.02112836, -14.20955114,
-7.66543414, -10.73250029, -14.20920819, -5.27444818, -10.0430456,
-7.29558638, -13.72777957, -16.58168235, -4.44479012, -14.77699067,
-11.55023034, -14.37873904, -13.80156743, -17.03877898, -13.79688572,
-8.34318779, -15.93602449, -14.96785248, -4.20489867, -6.34260421,
-8.6836805, -12.40561434, -11.65899316, -19.48056034, -13.24884988,
-12.03800774, -9.85039603, -7.51309429, -9.20345638, -15.12545221,
-11.15248177, -11.70093429]) # degrees
hist, r_edges, theta_edges = np.histogram2d(r, th, range=[r_range, theta_range], bins=[n_rings, n_segments])
r_range = [0.2, 0.3]
theta_range = [-25, 0]
idx = np.where((r <= 0.3) & (r > 0.2))[0]
hist, r_edges, theta_edges = np.histogram2d(r[idx], th[idx], range=[r_range, theta_range], bins=[1, 10])
hist = masked_array(hist, hist == 0) # remove empty bins
theta_mesh, r_mesh = np.meshgrid(theta_edges, r_edges)
pcm = ax.pcolormesh(theta_mesh*np.pi/180, r_mesh, hist, cmap=cmap, shading='flat')
Solution
Changing the dimensions of C to match X, Y fixed the issue. Since the histogram is one dimensional along one axis, tiling the array allows gouraud shading work as intended.
hist = np.tile(hist, (2, 1))
pcm = ax.pcolormesh(theta_mesh[:,:-1]*np.pi/180, r_mesh[:,:-1], hist, cmap=cmap, shading='gouraud')
Answered By - freja
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.