我有一个比例表,我正在为这些值制作一个choropleth映射,如下所示:
然后我要创建一个分别具有上限1和下限-1的颜色条。
event_y = (datetime.date.fromisoformat(start_date) + relativedelta(months = 2)).year
cdict = {'green': ((0.0, 0.0, 0.0), # no red at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.8, 0.8)), # set to 0.8 so its not too bright at 1
'red': ((0.0, 0.8, 0.8), # set to 0.8 so its not too bright at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)), # no green at 1
'blue': ((0.0, 0.0, 0.0), # no blue at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)) # no blue at 1
}
# Create the colormap using the dictionary
GnRd = colors.LinearSegmentedColormap('GnRd', cdict)
fig, ax = plt.subplots(1, figsize=(10,10))
ax.axis('off')
ax.set_title(f'Change in Proportion of {race} Drivers Stopped By Service Area\n Event: {event} ({event_y})'.format(race,event,event_y), fontdict={'fontsize':'15','fontweight' : '3'})
sm = plt.cm.ScalarMappable(cmap=GnRd, norm=plt.Normalize(vmin=-1, vmax=1,clip=True))
fig.colorbar(sm)
heat_diff.plot(column='prop', cmap=GnRd, linewidth=0.8, ax=ax, edgecolor='0.8')
ScalarMappable需要一个规范化参数,然后更改我正在绘制的真实值,有没有一种方法可以不进行标准化而使真实值保持不变?颜色的上限和下限分别为1和负1?
GeoPandas本机支持颜色条,您不必自己构建。以下应该起作用。
GnRd = colors.LinearSegmentedColormap('GnRd', cdict)
fig, ax = plt.subplots(1, figsize=(10,10))
ax.axis('off')
ax.set_title(f'Change in Proportion of {race} Drivers Stopped By Service Area\n Event: {event} ({event_y})'.format(race,event,event_y), fontdict={'fontsize':'15','fontweight' : '3'})
heat_diff.plot(column='prop', cmap=GnRd, linewidth=0.8, ax=ax,
edgecolor='0.8', vmin=-1, vmax=1, legend=True)
我刚刚将vmin=-1, vmax=1, legend=True
添加到.plot
,其中前两个限制了值的范围,legend
显示了色标。