我按照底图绘制地图如下:
plt.figure(figsize=(7,6))
m = Basemap(projection='cyl',llcrnrlat=40.125,urcrnrlat=44.625,\
llcrnrlon=-71.875,urcrnrlon=-66.375,resolution='h')
m.drawparallels(np.arange(int(40.125),int(44.625),1),labels=[1,0,0,0])
m.drawmeridians(np.arange(int(-71.875),int(-66.375),1),labels=[0,0,0,1])
m.drawcoastlines()
m.fillcontinents(color='grey')
m.drawmapboundary()
图片如下:
如何在保持轴上的刻度线的同时删除实际的纬度/经度线?
答案 0 :(得分:6)
通过将linewidth
参数设置为零
m.drawparallels(np.arange(int(40.125),int(44.625),1),labels=[1,0,0,0], linewidth=0.0)
m.drawmeridians(np.arange(int(-71.875),int(-66.375),1),labels=[0,0,0,1], linewidth=0.0)
答案 1 :(得分:3)
我发现底图刻度设置很混乱。
下图显示了8种不同的设置。一些符号:
ST=T
:suppress_ticks=True
。ST=F
:suppress_ticks=False
。DP-DR
:drawparallels()
和drawmeridians()
。DP-DR(LW=0)
:drawparallels(...,linewidth=0)
和drawmeridians(...,linewidth=0)
。mill
:米勒地图投影。在顶部面板中解释结果:
suppress_ticks=True
(默认值)以及drawparallels()
和drawmeridians()
。linewidth=0
设置为drawparallels()
和drawmeridians()
会删除网格线,但不添加刻度线。请注意,问题的标题是“如何在使刻度线保持在轴上的同时删除实际的纬度/经度线”。suppress_ticks=False
不会删除网格线,而是会添加额外的刻度线。这就是suppress_ticks
默认为True
的原因。linewidth=0
实际上是删除网格线的地方。再次suppress_ticks=False
引入了额外的刻度线,它们与drawparallels()
和drawmeridians()
添加的刻度线重叠。第二个面板:
drawparallels()
和drawmeridians()
并让其自动添加刻度,该怎么办?第一个缺点是您无法控制要添加的刻度。另一个是刻度标签的格式没有W/E
,N/S
符号(如果将labelstyle='+/-'
添加到drawparallels()
或drawmeridians()
,则标签将使用{{ 1}}格式)。如(f)所示,还有另一个很大的缺点。+ 41^{\circ}
地图投影而不是mill
。请注意,自动标注使用的是米而不是纬度/经度。不好(至少在这种情况下)。cyl
和ax.set_xticks(lon_ticks)
手动设置刻度来尝试添加刻度。刻度线已添加,但标签重叠。ax.set_yticks(lat_ticks)
投影再次提出的解决方案:mill
最后,创建图形的脚本:
m = Basemap(projection='mill',llcrnrlat=40.125,urcrnrlat=44.625,\
llcrnrlon=-71.875,urcrnrlon=-66.375,resolution='l',
ax=ax8, fix_aspect=False,
suppress_ticks=True)
m.drawcoastlines()
lat_ticks=np.arange(np.ceil(40.125),int(44.625),1)
lon_ticks=np.arange(np.ceil(-71.875),int(-66.375),1)
# convert from degree to map projection
lon_ticks_proj, _=m(lon_ticks, np.zeros(len(lon_ticks)))
_, lat_ticks_proj=m(np.zeros(len(lat_ticks)), lat_ticks)
# manually add ticks
ax8.set_xticks(lon_ticks_proj)
ax8.set_yticks(lat_ticks_proj)
ax8.tick_params(axis='both',which='major')
# add ticks to the opposite side as well
ax8.xaxis.set_ticks_position('both')
ax8.yaxis.set_ticks_position('both')
# remove the tick labels
ax8.xaxis.set_ticklabels([])
ax8.yaxis.set_ticklabels([])
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0,
xoffset=0.03*abs(m.xmax-m.xmin)) # need to enlarge the offset a bit
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0,
yoffset=0.03*abs(m.ymax-m.ymin)) # need to enlarge the offset a bit
更新:使用import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def getBM(ax, suppress_ticks, projection='cyl'):
m = Basemap(projection=projection,llcrnrlat=40.125,urcrnrlat=44.625,\
llcrnrlon=-71.875,urcrnrlon=-66.375,resolution='l',
ax=ax, fix_aspect=False,
suppress_ticks=suppress_ticks)
m.drawcoastlines()
return m
lat_ticks=np.arange(np.ceil(40.125),int(44.625),1)
lon_ticks=np.arange(np.ceil(-71.875),int(-66.375),1)
figure=plt.figure(figsize=(14,7),dpi=100)
nrows=2; ncols=4
#------------------------s1------------------------
ax1=figure.add_subplot(nrows,ncols,1)
m = getBM(ax1, True)
m.drawparallels(lat_ticks,labels=[1,0,0,0])
m.drawmeridians(lon_ticks,labels=[0,0,0,1])
ax1.set_title('(a) ST=T, DP-DR')
#------------------------s2------------------------
ax2=figure.add_subplot(nrows,ncols,2)
m = getBM(ax2, True)
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0)
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0)
ax2.set_title('(b) ST=T, DP-DR(LW=0)')
#------------------------s3------------------------
ax3=figure.add_subplot(nrows,ncols,3)
m = getBM(ax3, False)
m.drawparallels(lat_ticks,labels=[1,0,0,0])
m.drawmeridians(lon_ticks,labels=[0,0,0,1])
ax3.set_title('(c) ST=F, DP-DR')
#------------------------s4------------------------
ax4=figure.add_subplot(nrows,ncols,4)
m = getBM(ax4, False)
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0)
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0)
ax4.set_title('(d) ST=F, DP-DR(LW=0)')
#------------------------s5------------------------
ax5=figure.add_subplot(nrows,ncols,5)
m = getBM(ax5, False)
ax5.set_title('(e) ST=F')
#------------------------s6------------------------
ax6=figure.add_subplot(nrows,ncols,6)
m = getBM(ax6, False, projection='mill')
ax6.set_title('(f) ST=F, mill')
#------------------------s7------------------------
ax7=figure.add_subplot(nrows,ncols,7)
m = getBM(ax7, True)
ax7.set_xticks(lon_ticks)
ax7.set_yticks(lat_ticks)
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0)
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0)
ax7.set_title('(g) ST=T, DP-DR(LW=0), manual tick')
#------------------------s8------------------------
ax8=figure.add_subplot(nrows,ncols,8)
m = getBM(ax8, True, projection='mill')
# convert from degree to map projection
lon_ticks_proj, _=m(lon_ticks, np.zeros(len(lon_ticks)))
_, lat_ticks_proj=m(np.zeros(len(lat_ticks)), lat_ticks)
# manually add ticks
ax8.set_xticks(lon_ticks_proj)
ax8.set_yticks(lat_ticks_proj)
ax8.tick_params(axis='both',which='major')
# add ticks to the opposite side as well
ax8.xaxis.set_ticks_position('both')
ax8.yaxis.set_ticks_position('both')
# remove the tick labels
ax8.xaxis.set_ticklabels([])
ax8.yaxis.set_ticklabels([])
m.drawparallels(lat_ticks,labels=[1,0,0,0],linewidth=0,
xoffset=0.03*abs(m.xmax-m.xmin)) # need to enlarge the offset a bit
m.drawmeridians(lon_ticks,labels=[0,0,0,1],linewidth=0,
yoffset=0.03*abs(m.ymax-m.ymin)) # need to enlarge the offset a bit
ax8.set_title('(g) ST=T, DP-DR(LW=0), manual tick*')
figure.tight_layout()
figure.show()
和drawparallels(..., linewidth=0)
时,保存的PDF文件存在一些问题。在https://github.com/matplotlib/basemap/issues/493中提供了更多详细信息。
如果您遇到类似的问题,请考虑改用drawmeridians(..., linewidth=0)
。
答案 2 :(得分:2)
在basemap
中,您可以设置suppress_ticks=False
并获得不带线条的刻度。