0

I have a time-longitude array which I am plotting using the matplotlib contourf function. My longitude values span from [-180, 180] and as such appear on the x-axis in this order.

I would like my x-axis to run from 0 degrees to 0 degrees, so my x-axis ticks would be (0, 60, 120, 180, -120, -60, 0). Is there an easy way to do this?

My current code is:

levels = np.arange(0, 5+0.5, 0.5)
lon_ticks = np.array([0, 60, 120, 180, -120, -60, 0])

for i in range(3):
    fig = plt.figure(figsize = (15, 15))
    ax = fig.add_subplot(1, 1, 1)
    im = ax.contourf(lon,date_list,TRMM_lat_mean[:,:,i], 
                 levels = levels, extend = 'both', cmap = 'gist_ncar')
    cb = plt.colorbar(im)


    plt.savefig("C:/Users/amcna/Desktop/fig{number}.png".format(number = i)) 

Which outputs:

!(https://i.stack.imgur.com/64F5V.jpg)

As you can see my longitude array spans from [-180, 180], however I wish it to be arranged in the order I specified above.

1 Answers1

0

Since your data is cyclic, a representation through polar coordinates might work:

Example:

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.radians([0, 60, 120, 180, -120, -60, 0])
y = np.arange(0, 5+0.5, 0.5)

X, Y = np.mesh

grid(x, y)
Z = f(X, Y)

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(Y, X, Z)

plt.show()

enter image description here

If you don't want to do that, this thread might help you: Handling cyclic data with matplotlib contour/contourf

ttreis
  • 131
  • 7