【问题标题】:can't show 0 tick in right when central_longitude=180当 central_longitude=180 时无法在右侧显示 0 刻度
【发布时间】:2019-10-18 02:44:18
【问题描述】:

当其他条件如 +180 和 -180 时,它会变成正确的。 当 0 应该在右边时,它没有出现。

没有正确的0,网站的结果是一样的

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

我已阅读 geoaxes 的代码并尝试另一种方法在右侧显示 0。

如果使用matplotlib的方法结果更错误。

如果使用 Cartopy 地图网格线和刻度标签,结果是一样的。

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')

如何让 0 显示在轴的右侧?

【问题讨论】:

    标签: python-3.x matplotlib cartopy


    【解决方案1】:

    在我看来,应该出现在右侧的零也显示在左侧;请注意,ticklabel 如何比其他标签更大胆?!

    原因似乎是 360 % 360 == 0,所以刻度围绕开始旋转。

    一种解决方法是将最后一个刻度设置为稍微移动,例如

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

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

    例子:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2019-11-06
      • 2012-05-08
      • 1970-01-01
      相关资源
      最近更新 更多