Search code examples
pythoncartopy

Weird setting of latitude labels in cartopy Miller projection


Just to make things easy I have reproduced my problem adapting an example from the gallery of cartopy's latest release

fig = plt.figure(figsize=(8, 10))

miller = ccrs.Miller(central_longitude=180)

ax = fig.add_subplot(1, 1, 1, projection=miller)

ax.set_global()
ax.coastlines()
ax.set_yticks(np.arange(-90, 90.5, 30), crs=miller)
lat_formatter = LatitudeFormatter()
ax.yaxis.set_major_formatter(lat_formatter)

plt.show()

enter image description here

For some reason the y axis labels are changed and have weird values. Potentially something that has to do with LatitudeFormatter ?

Important: For some environment related reasons I'm using cartopy 0.18.0b3.dev15+


Solution

  • Cartopy is giving you exactly what you asked for, which is labels at (-90, -60, -30, 0, 30, 60, 90) in the Miller projection, i.e. not in degrees of latitude. Because you are using the LatitudeFormatter it is converting these Miller projection points into degrees latitude for you for display.

    What it looks like you wanted to do was label in a lat/long coordinate system, so you should use ccrs.PlateCarree() as the crs argument when creating the ticks, like this:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    from cartopy.mpl.ticker import LatitudeFormatter
    import numpy as np
    
    fig = plt.figure(figsize=(8, 10))
    
    miller = ccrs.Miller(central_longitude=180)
    
    ax = fig.add_subplot(1, 1, 1, projection=miller)
    
    ax.set_global()
    ax.coastlines()
    ax.set_yticks(np.arange(-90, 90.5, 30), crs=ccrs.PlateCarree())
    lat_formatter = LatitudeFormatter()
    ax.yaxis.set_major_formatter(lat_formatter)
    
    plt.show()
    

    Miller projection with latitude labels