为什么不能从cartopy-geopandas图中将特定的轴刻度标签设置为“ False”(例如:xlabels_top,ylabels_right)?

时间:2019-02-07 18:49:47

标签: python-3.x matplotlib geopandas cartopy

从我的Geopandas图中将Falsexlabels_topylabels_right设置为Geoaxes时,我遇到了严重的困难。

此大熊猫纲图是在PlateCarree子图内制作的,该子图是根据Cartopy库的Geodataframe投影创建的。

我的地理熊猫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() 以SIRGAS 2000(单位:度)表示,EPSG:4989。 因此,我从cartopy库中创建了Geodetic Globe对象。

这是一个代码段:

False

这里是一个数字示例。可以注意到,上轴的xticks和右轴的yticks尚未关闭(func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { //characterSet that holds digits var allowed = CharacterSet.decimalDigits //initialize the period for use as a characterSet let period = CharacterSet.init(charactersIn: ".") //adding these two characterSets together allowed.formUnion(period) //all the characters not in the allowed union let inverted = allowed.inverted //if latest input is from the characters not allowed is present (aka, empty), do not change the characters in the text range if string.rangeOfCharacter(from: inverted) != nil { return false } //if the text already contains a period and the string contains one as well, do not change output else if (textField.text?.contains("."))! && string.contains(".") { return false } //however, if not in the inverted set, allow the string to replace latest value in the text else { return true } )。

Figure with xticks and yticks error

因此,我想知道这是Cartopy和Geopandas之间的问题,还是我的代码有误?

2 个答案:

答案 0 :(得分:1)

标签属于gridliner实例而不是轴,可以通过存储gridlines方法返回的gridliner并在其中设置top_labelsright_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()

答案 1 :(得分:0)

我终于设法解决了我的问题。为此,我使用了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()