MatPlotLib + GeoPandas:绘制多层地图,控制图像大小

3

鉴于此处提供的shape文件 链接,我已经可以制作出需要的基本地图,并在地图上添加了县标签和一些点(如下所示)。但我遇到的问题是,似乎无法用figsize来控制图片的大小。

以下是我的代码:

import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline
figsize=5,5
fig = plt.figure(figsize=(figsize),dpi=300)

shpfileshpfile=r'Y:\HQ\TH\Groups\NR\PSPD\Input\US_Counties\cb_2015_us_county_20m.shp' 
c=gpd.read_file(shpfile)
c=c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075','26125','26163','26099','26115','26065'])]
c['coords'] = c['geometry'].apply(lambda x: x.representative_point().coords[:])
c['coords'] = [coords[0] for coords in c['coords']]
ax=c.plot()

#Control some attributes regarding the axis (for the plot above)   

ax.spines['top'].set_visible(False);ax.spines['bottom'].set_visible(False);ax.spines['left'].set_visible(False);ax.spines['right'].set_visible(False)
    ax.tick_params(axis='y',which='both',left='off',right='off',color='none',labelcolor='none')
    ax.tick_params(axis='x',which='both',top='off',bottom='off',color='none',labelcolor='none')
    for idx, row in c.iterrows():
        ax.annotate(s=row['NAME'], xy=row['coords'],
                     horizontalalignment='center')
lat2=[42.5,42.3]
lon2=[-84,-83.5]

   #Add another plot...

ax.plot(lon2,lat2,alpha=1,marker='o',linestyle='none',markeredgecolor='none',markersize=15,color='white')
plt.show()

如您所见,我选择通过轴名称来调用图表,因为我需要控制轴的属性,例如tick_params。我不确定是否有更好的方法。这似乎是一个“显而易见”的问题,但我无法弄清楚为什么我无法控制图形大小。

提前感谢!

1个回答

2
我只需要做以下几件事情:
  1. 使用fig, ax = plt.subplots(1, 1, figsize=(figsize))
  2. 在中使用ax=ax参数
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline
figsize=5,5
#fig = plt.figure(figsize=(figsize),dpi=300)
#ax = fig.add_subplot(111)
fig, ax = plt.subplots(1, 1, figsize = (figsize))
shpfileshpfile=r'Y:\HQ\TH\Groups\NR\PSPD\Input\US_Counties\cb_2015_us_county_20m.shp' 
c=gpd.read_file(shpfile)
c=c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075','26125','26163','26099','26115','26065'])]
c['coords'] = c['geometry'].apply(lambda x: x.representative_point().coords[:])
c['coords'] = [coords[0] for coords in c['coords']]
c.plot(ax=ax)
ax.spines['top'].set_visible(False);ax.spines['bottom'].set_visible(False);ax.spines['left'].set_visible(False);ax.spines['right'].set_visible(False)
ax.tick_params(axis='y',which='both',left='off',right='off',color='none',labelcolor='none')
ax.tick_params(axis='x',which='both',top='off',bottom='off',color='none',labelcolor='none')
for idx, row in c.iterrows():
    ax.annotate(s=row['NAME'], xy=row['coords'],
                 horizontalalignment='center')
lat2=[42.5,42.3]
lon2=[-84,-83.5]
ax.plot(lon2,lat2,alpha=1,marker='o',linestyle='none',markeredgecolor='none',markersize=15,color='white')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接