为什么不能从 cartopy-geopandas 图中设置为 "False" 特定轴刻度标签(例如:xlabels_top、ylabels_right)?
Why can't one set to "False" specific axis ticklabels (ex: xlabels_top, ylabels_right) from a cartopy-geopandas plot?
我在将我的 Geopandas 绘图中的 xlabels_top
和 ylabels_right
设置为 False
时遇到了严重困难。
这个 geopandas 图是在一个 Geoaxes
子图中制作的,该子图是使用来自 Cartopy 库的 PlateCarree
投影创建的。
我的 geopandas Geodataframe
使用 SIRGAS 2000(单位:度),EPSG:4989。
因此,我从 cartopy 库中创建了一个 Geodetic Globe 对象。
这是一个代码片段:
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 实例而不是轴,您可以通过存储 gridlines 方法返回的 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()
我在将我的 Geopandas 绘图中的 xlabels_top
和 ylabels_right
设置为 False
时遇到了严重困难。
这个 geopandas 图是在一个 Geoaxes
子图中制作的,该子图是使用来自 Cartopy 库的 PlateCarree
投影创建的。
我的 geopandas Geodataframe
使用 SIRGAS 2000(单位:度),EPSG:4989。
因此,我从 cartopy 库中创建了一个 Geodetic Globe 对象。
这是一个代码片段:
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 实例而不是轴,您可以通过存储 gridlines 方法返回的 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()