Search code examples
python-3.xmatplotlibcartopy

can't show 0 tick in right when central_longitude=180


when other condition like +180 and -180, it turns rigtht. when 0 should be in the right, it did't appear.

The website's results is the same without the right 0

https://scitools.org.uk/cartopy/docs/latest/gallery/tick_labels.html?highlight=tick

I've read the code of geoaxes and try another method to show the 0 in the rigtht.

If use the matplotlib method the results more wrong.

If use Cartopy map gridlines and tick labels, results is the same.

https://scitools.org.uk/cartopy/docs/latest/matplotlib/gridliner.html#cartopy.mpl.gridliner.Gridliner.xformatter

#===================================================
#plot the world map central 180 
#===================================================
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
def make_map(scale):
    fig=plt.figure(figsize=(8, 10))
    ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
    ax.set_global()
    land = cfeature.NaturalEarthFeature('physical', 'land', scale,edgecolor='face',
                                                              facecolor=cfeature.COLORS['land'])
    ax.add_feature(land, facecolor='0.75')
    ax.coastlines(scale)
    #===set tick
    ax.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())

    lon_formatter = LongitudeFormatter(zero_direction_label=False)
    lat_formatter = LatitudeFormatter()
    ax.xaxis.set_major_formatter(lon_formatter)
    ax.yaxis.set_major_formatter(lat_formatter)
    #=====set grid
    #gl = ax.gridlines()
    ax.grid()
    return fig,ax
fig,ax=make_map(scale='110m')

enter image description here

How do I get the 0 to show up on the right side of the axis?


Solution

  • To me it looks like the zero that is supposed to appear on the right is shown on the left as well; note how that ticklabel is a little bolder than the others?!

    enter image description here

    The reason seems that 360 % 360 == 0, so the tick revolves around to the start.

    A workaround would be to set the last tick slightly shifted, like

    [0, 60, 120, 180, 240, 300, 359.9999999999]
    

    or

    [0, 60, 120, 180, -120, -60, -1e-10]
    

    Example:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
    def make_map(scale):
        fig=plt.figure(figsize=(8, 6))
        ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
        ax.set_global()
        land = cfeature.NaturalEarthFeature('physical', 'land', scale,edgecolor='face',
                                            facecolor=cfeature.COLORS['land'])
        ax.add_feature(land, facecolor='0.75')
        ax.coastlines(scale)
    
        ax.set_xticks([0, 60, 120, 180, 240, 300, 359.9999999999], crs=ccrs.PlateCarree())
        ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
    
        lon_formatter = LongitudeFormatter(zero_direction_label=True)
        lat_formatter = LatitudeFormatter()
        ax.xaxis.set_major_formatter(lon_formatter)
        ax.yaxis.set_major_formatter(lat_formatter)
    
        ax.grid()
        return fig,ax
    
    fig,ax=make_map(scale='110m')
    plt.show()
    

    enter image description here