【问题标题】:MatPlotLib + GeoPandas: Plot Multiple Layers, Control FigsizeMatPlotLib + GeoPandas:绘制多个图层,控制 Figsize
【发布时间】:2017-01-06 00:18:58
【问题描述】:

鉴于可用的形状文件here:我知道可以生成我需要的带有县标签的基本地图,甚至地图上的一些点(见下文)。我遇到的问题是我似乎无法用 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。我不确定是否有更好的方法。这似乎是“不费吹灰之力”,但我似乎无法弄清楚为什么我无法控制图形大小。

提前致谢!

【问题讨论】:

    标签: python-3.x matplotlib figure geopandas


    【解决方案1】:

    我只需要执行以下操作:

    1. 使用fig, ax = plt.subplots(1, 1, figsize = (figsize))

    2.使用 c.plot() 中的 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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      相关资源
      最近更新 更多