【问题标题】:How to display a colorbar for a Basemap如何显示底图的颜色条
【发布时间】:2017-09-29 19:41:41
【问题描述】:

我有一个shapefile 和一个这样的系列:

UF
Acre                     261
Alagoas                  657
Amazonas                 793
Amapá                    162
Bahia                   1867
Ceará                   5657
Distrito Federal         430
Espírito Santo          1734
Goiás                   4110
Maranhão                1421
Minas Gerais           11812
Mato Grosso do Sul      1006
Mato Grosso             1391
Pará                    1889
Paraíba                 1575
Pernambuco              4019
Piauí                   1665
Paraná                  3745
Rio de Janeiro          1613
Rio Grande do Norte     1998
Rondônia                3102
Roraima                  237
Rio Grande do Sul       5643
Santa Catarina          5372
Sergipe                  413
São Paulo               8237
Tocantins                771
Name: 0, dtype: int64

UF 是 shapefile 中显示的州名,而系列值是我想用来生成颜色以填充底图的值。这是我到目前为止得到的:

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import Basemap
import matplotlib.cm as cm
import matplotlib as mpl
from matplotlib.collections import PatchCollection


def make_map(ax):
    m = Basemap(projection='merc', llcrnrlat=-35, urcrnrlat=7,
                llcrnrlon=-77, urcrnrlon=-32, resolution='i', ax=ax)
    m.fillcontinents()
    return m

def drawstates(fig, ax, data, shapefile='../BRA_adm_shp/BRA_adm1'):
    shp = m.readshapefile(shapefile, 'states', drawbounds=False)

    norm = mpl.colors.Normalize(vmin=data.min(), vmax=data.max())
    cmap = cm.hot
    sm = cm.ScalarMappable(norm=norm, cmap=cmap)
    colors = []
    patches = []

    for nshape, seg in enumerate(m.states):
        uf = m.states_info[nshape]['NAME_1']
        color  = sm.to_rgba(data[uf])
        poly = Polygon(seg, facecolor=color, edgecolor='white')
        ax.add_patch(poly)
        patches.append(poly)
        colors.append(color)

    p = PatchCollection(patches, cmap=cmap)
    p.set_array(np.array(colors))
    cb = fig.colorbar(p, ax=ax, orientation='horizontal')

fig, axes = plt.subplots(1, 2, figsize=(20, 10))
m = make_map(axes[0])
drawstates(fig, m.ax, m1)

这会导致:

我不确定这是否是正确的做法,但我想知道如何保持输入值的比例,即不在 0 和 1 之间缩放颜色条,以及如何防止这种大距离在地图与其颜色条之间。

【问题讨论】:

    标签: python matplotlib matplotlib-basemap colorbar


    【解决方案1】:

    根据 ScalaMappable sm 对多边形进行着色。因此,这个 ScalarMappable 是您想要作为颜色图参数的那个

    sm = cm.ScalarMappable(norm=norm, cmap=cmap)
    sm.set_array([]) # can be an empty list, only needed for matplotlib < 3.1
    # ...
    cb = fig.colorbar(sm, ax=ax, orientation='horizontal')
    

    坐标轴和颜色条之间的填充可以使用pad 参数设置。默认应该是pad=0.15,你需要自己找到一个好的值。

    【讨论】:

    • Pass sm 是我的第一次尝试。但它会导致错误:Colormap 无法识别。可能的值有:Accent、Accent_r、Blues 等...
    • 嗯? sm 你到底是从哪里传过来的?
    • 我有真正的问题相信将 scalarMappable 传递给 fig.colorbar 甚至可以产生此消息。它可以事先来自另一条线吗?您确定您的代码与问题中的完全相同吗?
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多