这个问题从另一个问题#1出现1:
我的问题关于X的版本和Y轴从cartopy-geopandas情节ticklabels。我想,根据一定的规则,以改变从每个我的ticklabels(X,和Y轴)的我的文本对象。
例如,我想从我的X和Y轴ticklabels改变小数分隔(“‘)转换成逗号分隔符(’,”)。
这里是不能做的代码:
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
### Creating a function to change my Ticklabels:
def Ticker_corrector(ax):
"""
Parameter:ax, axes whose axis X and Y should be applied the function
"""
## Correcting the Axis X and Y of the main Axes
Xticks = ax.get_xticklabels()
for i in Xticks:
T = i.get_text()
T = T.replace('.',',')
i = i.set_text(T)
print(T)
ax.set_xticklabels(Xticks)
## Correcting the Axis Y
Yticks = ax.get_yticklabels()
for i in Xticks:
T = i.get_text()
T = T.replace('.',',')
i = i.set_text(T)
print(T)
ax.set_yticklabels(Yticks)
return ax
Ax = Ticker_corrector(Ax)
Fig.show()
上面的代码中一个有趣的部分是,它运行没有问题。 Python的并不表示任何错误,并且绘制没有任何错误警告图。
尽管如此,Ticklabels保持不变。因此,我需要这个问题有所帮助。
我感谢您的时间。
您忠诚的,
我相信我已经找到了解决办法。它可能并不总是工作,但肯定解决我的问题。
该解决方案的基本依据是创造我的前后情节设置我的matplotlib的“语言环境”。
下面是一个例子:
import locale
locale.setlocale(locale.LC_ALL, "Portuguese_Brazil.1252")
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True
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
Fig.show()