Python 设置颜色栏的上限和下限时打印比例

Python 设置颜色栏的上限和下限时打印比例,python,matplotlib,geospatial,geopandas,choropleth,Python,Matplotlib,Geospatial,Geopandas,Choropleth,我有一个比例表,我正在制作一个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 cha

我有一个比例表,我正在制作一个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')

ScalarMapable需要一个normalize参数,该参数会更改我正在绘制的真值有没有一种方法可以在不进行规范化的情况下执行此操作,从而使真值保持不变?由于颜色的上限和下限分别为1和负1?

GeoPandas本机支持colorbar,所以您无需构建自己的颜色栏。下面的方法应该有效

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
显示了颜色栏。

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
显示了色条