如何在 Python-Matplotlib-Basemap 中绘制特定形状的 contourf 图

How to draw contourf plot for a particular shape in Python-Matplotlib-Basemap

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

(来源:tietuku.com

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

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

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)

这是关于如何执行此操作的大纲。经过一些修改,我想你会上路的。