我已经为绘制美国各县内的痕量气体浓度做了类似的事情,我认为这与您正在尝试做的事情非常相似。
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)
这是您如何执行此操作的一般大纲。经过一些修改,我认为你会在路上。