我有在设定的严重困难False
的xlabels_top
和ylabels_right
从我Geopandas情节。
这geopandas情节是一个内部制作Geoaxes
与创建插曲PlateCarree
从Cartopy库投影。
我geopandas Geodataframe
在地心测量区域系统2000(单位:度),EPSG:4989.所以我从cartopy库中创建一个大地环球对象。
下面的代码片段:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import geopandas as gpd
Geopandas_DF = gpd.read_file('my_file.shp')
# setting projection and Transform
Projection=ccrs.PlateCarree()
Transform = ccrs.Geodetic(globe=ccrs.Globe(ellipse='GRS80'))
Fig, Ax = plt.subplots(1,1, subplot_kw={'projection': Projection})
Geopandas_DF.plot(ax=Ax, transform=Ax.transData)
Ax.gridlines(crs=Projection , draw_labels=True, linewidth=0.5,
alpha=0.4, color='k', linestyle='--')
Ax.xlabels_top = False nn# It should turn off the upper x ticks
Ax.ylabels_right = False # It should turn off the right y ticks
Ax.ylabels_left = True
Ax.xlines = True
Fig.show()
这里是一个图形例。 可以注意到,从上轴xticks和yticks从右侧轴尚未关闭( False
)。
因此,我想知道这是否是Cartopy和Geopandas之间的问题,还是我做错事在我的代码。
标签属于gridliner实例不是轴,可以通过存储由网格线,并设定返回gridliner关闭它们有top_labels
, right_labels
如:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import geopandas as gpd
Geopandas_DF = gpd.read_file('my_file.shp')
# setting projection and Transform
Projection=ccrs.PlateCarree()
Transform = ccrs.Geodetic(globe=ccrs.Globe(ellipse='GRS80'))
Fig, Ax = plt.subplots(1,1, subplot_kw={'projection': Projection})
Geopandas_DF.plot(ax=Ax, transform=Ax.transData)
gl = Ax.gridlines(crs=Projection , draw_labels=True, linewidth=0.5,
alpha=0.4, color='k', linestyle='--')
# For Cartopy <= 0.17
gl.xlabels_top = False
gl.ylabels_right = False
# For Cartopy >= 0.18
# gl.top_labels = False
# gl.right_labels = False
Fig.show()
我终于成功地解决我的问题。 我用Ajdawson技巧,以做到这一点。 下面是解决方案的代码:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import geopandas as gpd
Geopandas_DF = gpd.read_file('my_file.shp')
# setting projection and Transform
Projection=ccrs.PlateCarree()
Transform = ccrs.Geodetic(globe=ccrs.Globe(ellipse='GRS80'))
Fig, Ax = plt.subplots(1,1, subplot_kw={'projection': Projection})
Geopandas_DF.plot(ax=Ax, transform=Ax.transData)
gl = Ax.gridlines(crs=Projection , draw_labels=True, linewidth=0.5,
alpha=0.4, color='k', linestyle='--')
gl.top_labels = False
gl.right_labels = False
gl.xlabels_top = False
gl.ylabels_right = False
Fig.show()
文章来源: Why can't one set to “False” specific axis ticklabels (ex: xlabels_top, ylabels_right) from a cartopy-geopandas plot?