Search code examples
pythonmatplotlibplotpolar-coordinatescontourf

Matplotlib polar contourf plot: continuous across theta origin


I have data in format E(freq, theta), where E is a 2D array and freq and theta are 1D arrays.

The following portion of code produces the attached figure. However, I would like to make the contourf plot continuous across the 0-degree origin (i.e. no wedge of white space along the 0 azimuth).

I've explored the matplotlib documentation, and posted questions very extensively and can't seem to find a solution for this issue. Any ideas?

Code:

[r, th] = np.meshgrid(freq,theta)

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)

cntf = ax.contourf(th,r,np.log10(E),cmap='jet',extend='both',
    levels=np.linspace(np.mean(np.log10(E)), np.amax(np.log10(E)), 15))

ax.set_rlim(0, .3)
label_position=ax.get_rlabel_position()
ax.text(np.radians(label_position+25),ax.get_rmax()/1.5,'f (Hz)',
        rotation=label_position,ha='center',va='center')

Produced plot: Directional wave spectrum


Solution

  • Something similar to this: https://stackoverflow.com/a/22129714/9324652

    enter image description here

    dtheta = np.diff(theta).mean()
    wrp_theta = np.concatenate((theta, theta[-1:] + dtheta))
    wrp_E = np.concatenate((E, E[0:1, :]), axis=0)