def plot_choropleth(gdf,
                    parameter,
                    title,
                    cmap,
                    outdir,
                    country_iso3,
                    filename,
                    norm=None,
                    use_scheme=False):
    fig, ax = plt.subplots()
    if use_scheme:
        try:
            scheme = mc.FisherJenks(gdf[parameter], k=5)
        except ValueError:
            logger.warning(f'Not enough values to plot {filename}')
            return 0
        # Make nice legend labels
        if gdf[parameter].max() < 10:
            format_bin = lambda bin: f'{np.round(bin, 1)}'
        else:
            precision = len(str(int(scheme.bins[0]))) - 2
            format_bin = lambda bin: f'{int(np.round(bin, -precision)):,}'
        legend_labels = [f'< {format_bin(scheme.bins[0])}'] + \
                        [f'{format_bin(scheme.bins[i] + 1)}–{format_bin(scheme.bins[i + 1])}'
                         for i in range(len(scheme.bins) - 2)] + \
                        [f'> {format_bin(scheme.bins[-2])}']
        legend_kwargs = ({'title': title})
    else:
        scheme = None
        legend_labels = None
        legend_kwargs = ({
            'orientation': 'horizontal',
            'label': title,
            'shrink': 0.5
        })
    if norm is not None:
        norm = mpl.colors.Normalize(vmin=norm[0], vmax=norm[1])
    gplt.choropleth(gdf,
                    ax=ax,
                    legend=True,
                    lw=0.5,
                    hue=parameter,
                    cmap=cmap,
                    legend_kwargs=legend_kwargs,
                    scheme=scheme,
                    legend_labels=legend_labels,
                    norm=norm)
    fig.savefig(os.path.join(outdir, f'{country_iso3}_{filename}.png'),
                dpi=150)
    plt.close(fig)
コード例 #2
0
def pretty_fisherjenks(y, k=5, digits=2):
    """
    Return pretty, rounded, Fisher Jenks classification schemes. For fast classifications use pretty_fisherjenks_sampled. 
    Relies on mapclassify. 
    -----------
    Parameters:
    y: input vector
    k: number of classes
    digits: degree of rounding
    -----------
    Returns: mapclassify object
    """
    original = mapclassify.FisherJenks(y, k=k)
    accuracies = (-(np.floor(np.log10(original.bins)) - digits)).astype(int)
    pretty_bins = [
        round(limit, accuracies[i]) for i, limit in enumerate(original.bins)
    ]
    return mapclassify.UserDefined(y, pretty_bins)
コード例 #3
0
def geospatial_viz(geo_data_url,
                   point_data_url=None,
                   att_var=None,
                   map_type=None):
    '''
    function to visualize the attribute information in map. (eg, population in states)
    geo_att_data: geodataframe that contains both geometry and attributes info
    att_var: the attributes to be visualized in the map
    map_type: string, the type of map to be viz. pointplot, choropleth, voronoi
    
    if point_data = None, att_var must be from geo_data
    
    '''
    geo_data = gpd.read_file(geo_data_url)
    print(geo_data.head())

    if point_data_url == 'No point attribute data':
        if att_var is None:
            ax = gplt.polyplot(geo_data, figsize=(10, 5))
            ax.set_title('plain map of continental USA', fontsize=16)
        else:
            if map_type == 'choropleth':
                scheme = mc.FisherJenks(geo_data[att_var], k=5)
                labels = scheme.get_legend_classes()
                ax = gplt.polyplot(geo_data, projection=gcrs.AlbersEqualArea())
                gplt.choropleth(geo_data,
                                hue=att_var,
                                edgecolor='white',
                                linewidth=1,
                                cmap='Blues',
                                legend=True,
                                scheme=scheme,
                                legend_labels=labels,
                                ax=ax)
                ax.set_title('{} in the continental US'.format(att_var),
                             fontsize=16)

            if map_type == "cartogram":
                gplt.cartogram(geo_data,
                               scale=att_var,
                               edgecolor='black',
                               projection=gcrs.AlbersEqualArea())

    else:
        point_data = gpd.read_file(point_data_url)
        scheme = mc.Quantiles(point_data[att_var], k=5)
        labels = scheme.get_legend_classes()

        if map_type == 'pointplot':
            if isinstance(point_data.geometry[0],
                          shapely.geometry.point.Point):
                ax = gplt.polyplot(geo_data,
                                   edgecolor='white',
                                   facecolor='lightgray',
                                   figsize=(12, 8)
                                   #projection = gcrs.AlbersEqualArea()
                                   )
                gplt.pointplot(point_data,
                               ax=ax,
                               hue=att_var,
                               cmap='Blues',
                               scheme=scheme,
                               scale=att_var,
                               legend=True,
                               legend_var='scale',
                               legend_kwargs={"loc": 'lower right'},
                               legend_labels=labels)
                ax.set_title(
                    'Cities in the continental US, by population 2010',
                    fontsize=16)
            else:
                print('Geometry data type not valid')

        if map_type == "voronoi":
            # check uniqueness of coordinates
            duplicates = point_data.geometry.duplicated()
            point_data_unique = point_data[-duplicates]
            proj = gplt.crs.AlbersEqualArea(central_longitude=-98,
                                            central_latitude=39.5)

            ax = gplt.voronoi(point_data_unique,
                              hue=att_var,
                              clip=geo_data,
                              projection=proj,
                              cmap='Blues',
                              legend=True,
                              edgecolor="white",
                              linewidth=0.01)

            gplt.polyplot(geo_data,
                          ax=ax,
                          extent=geo_data.total_bounds,
                          edgecolor="black",
                          linewidth=1,
                          zorder=1)
            plt.title("{} in US cities".format(att_var), fontsize=16)
コード例 #4
0
)
axarr[0][0].set_title('scheme=None', fontsize=18)

scheme = mc.Quantiles(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[0][1]
)
axarr[0][1].set_title('scheme="Quantiles"', fontsize=18)

scheme = mc.EqualInterval(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[1][0]
)
axarr[1][0].set_title('scheme="EqualInterval"', fontsize=18)

scheme = mc.FisherJenks(cali.area, k=5)
gplt.choropleth(
    cali, hue='area', linewidth=0, scheme=scheme, ax=axarr[1][1]
)
axarr[1][1].set_title('scheme="FisherJenks"', fontsize=18)

plt.subplots_adjust(top=0.92)
plt.suptitle('California State Districts by Area, 2010', fontsize=18)

# %% [markdown] Collapsed="false"
# ## Spatial Merge

# %% [markdown] Collapsed="false"
# Subset to Africa

# %% Collapsed="false"
コード例 #5
0
#optimization styles in geopandas
covidMap.plot(column='CasesPer100k',
              scheme='FisherJenks',
              k=5,
              cmap='OrRd',
              legend=True)
#%%
covidMap.plot(column='CasesPer100k',
              scheme='HeadTailBreaks',
              cmap='OrRd',
              legend=True)
#%%
#optimization styles in geoplot
gplt.choropleth(covidMap,
                hue='CasesPer100k',
                scheme=mc.FisherJenks(covidMap.CasesPer100k, k=5),
                cmap='OrRd',
                legend=True)
#%%
gplt.choropleth(covidMap,
                hue='CasesPer100k',
                scheme=mc.HeadTailBreaks(covidMap.CasesPer100k),
                cmap='OrRd',
                legend=True)

#%%
#new variable
valuesNew = 100000 * (covidMap.Deaths / covidMap.Population)
covidMap['DeathsPer100k'] = valuesNew

#%%
コード例 #6
0
def test_classify():
    # data
    link_to_data = examples.get_path('columbus.shp')
    gdf = gpd.read_file(link_to_data)
    x = gdf['HOVAL'].values

    # box_plot
    a = classify(x, 'box_plot') 
    b = mapclassify.BoxPlot(x)
    _assertions(a, b)

    # EqualInterval
    a = classify(x, "EqualInterval", k=3)
    b = mapclassify.EqualInterval(x, k=3)
    _assertions(a, b)

    # FisherJenks
    a = classify(x, "FisherJenks", k=3)
    b = mapclassify.FisherJenks(x, k=3)
    _assertions(a, b)

    
    a= classify(x, "FisherJenksSampled", k=3, pct_sampled=0.5, truncate=False)
    b = mapclassify.FisherJenksSampled(x, k=3, pct=0.5,truncate=False)
    _assertions(a, b)
    
    # headtail_breaks
    a = classify(x, 'headtail_breaks')
    b = mapclassify.HeadTailBreaks(x)
    _assertions(a, b)
    
    # quantiles
    a = classify(x, 'quantiles',k=3)
    b = mapclassify.Quantiles(x, k=3)
    _assertions(a, b)

    # percentiles
    a = classify(x, 'percentiles', pct=[25,50,75,100])
    b = mapclassify.Percentiles(x, pct=[25,50,75,100])
    _assertions(a, b)

    #JenksCaspall
    a = classify(x, 'JenksCaspall', k=3)
    b = mapclassify.JenksCaspall(x, k=3)
    _assertions(a, b)

    a = classify(x, 'JenksCaspallForced', k=3) 
    b = mapclassify.JenksCaspallForced(x, k=3)
    _assertions(a, b)
    
    a = classify(x, 'JenksCaspallSampled', pct_sampled=0.5)
    b = mapclassify.JenksCaspallSampled(x, pct=0.5)
    _assertions(a, b)
    

    # natural_breaks, max_p_classifier
    a = classify(x, 'natural_breaks')
    b = mapclassify.NaturalBreaks(x)
    _assertions(a, b)

    
    a = classify(x, 'max_p', k=3, initial=50)
    b = mapclassify.MaxP(x, k=3, initial=50)
    _assertions(a, b)
    

    # std_mean
    a = classify(x, 'std_mean', multiples=[-1,-0.5,0.5,1])
    b = mapclassify.StdMean(x, multiples=[-1,-0.5,0.5,1])
    _assertions(a, b)

    
    # user_defined
    a = classify(x, 'user_defined', bins=[20, max(x)]) 
    b = mapclassify.UserDefined(x, bins=[20, max(x)])
    _assertions(a, b)