【问题标题】:How to draw contourf plot for a particular shape in Python-Matplotlib-Basemap如何在 Python-Matplotlib-Basemap 中为特定形状绘制轮廓图
【发布时间】:2015-11-20 13:02:48
【问题描述】:

例如,我有一个 3-D 数组数据表示所示区域中的化学浓度,如:

(来源:tietuku.com

我想将它绘制在属于该域的某个行政区划(不是正方形)中。
现在我可以在 Basemap 中读取和绘制 shapefile,但我找不到在它之外绘制一些元素的方法?
如果可能,如何使图形尺寸更小?因为当我在底图中添加 shapefile 时,输出文件要大得多。
希望得到您的回复!谢谢!

【问题讨论】:

    标签: python matplotlib plot gis matplotlib-basemap


    【解决方案1】:

    我已经为绘制美国各县内的痕量气体浓度做了类似的事情,我认为这与您正在尝试做的事情非常相似。

    import pandas as pd, numpy as np, datetime as dt, pytz
    from mpl_toolkits.axes_grid1.inset_locator import inset_axes
    from mpl_toolkits.basemap import Basemap
    from os import getcwd, chdir
    from pylab import *
    
    
    # create figure
        fig = figure( figsize=(6.25,4.5) )
        ax  = fig.add_subplot(111)
    
        m = Basemap(projection='cyl', llcrnrlat=minlat, urcrnrlat=maxlat, llcrnrlon=minlon, urcrnrlon=maxlon, resolution='i')
    
        lw = 0.1
        m.drawcoastlines(linewidth=lw)
        m.drawparallels(np.arange(30, 50, 2), labels=[1,0,0,1], fontsize=6, labelstyle='', rotation=0, linewidth=0.25)
        m.drawmeridians(np.arange(-86,-70,2), labels=[1,0,0,1], fontsize=6, labelstyle='', rotation=45, linewidth=0.25)
        m.drawstates(linewidth=lw)
        m.drawcountries(linewidth=lw)
        m.drawcounties(linewidth=0.01)
    
    
        ax2 = gca()
    
        a = np.ones( (10,10) )
        b = a*10
        cb = ax.scatter(x=a, y=a, s=1, c=b, vmin=0, vmax=10)   # create dummy plot to allow colorbar insertion
    
        # insert colorbar
        axins = inset_axes(ax,
                       width="5%", # width = 10% of parent_bbox width
                       height="100%", # height : 50%
                       loc=6,
                       bbox_to_anchor=(1.05, 0., 1, 1),
                       bbox_transform=ax.transAxes,
                       borderpad=0,
                   )
        cbar = colorbar(cb, cax=axins)
        cbar.set_label('NO\$\mathrm{_2}$ (ppb)', fontsize=8)
        cbar.ax.tick_params(labelsize=8)
    
    # fill in counties
    # I think this is the part you are most interested in
    county_mean = county_data.mean()
    
    # since I want to color within the map to match a specific color 
    # within the cbar, I define it here based on the cbar's color coding
    if pd.isnull(county_mean.no2)[0]:
        color = (1,1,1)
    else:
        color = cbar.to_rgba(county_mean.no2[0])
    
    # get the polygon values that define the county boundaries
    # here, "r" represents the county PID (identifier)
    poly = Polygon(m.counties[r], facecolor=color, edgecolor='k')
    ax2.add_patch(poly)
    

    这是您如何执行此操作的一般大纲。经过一些修改,我认为你会在路上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2014-10-16
      • 2012-05-27
      相关资源
      最近更新 更多