Search code examples
pythonmapscluster-analysisgeopandaspysal

How to define colors of Lisa cluster manually?


I am trying to do some maps of LISA CLUSTERS. O changed the code of lisa_cluster function to specify the colors I wanted. I used a generic 5 colors list, and changed it manually

from matplotlib import patches, colors
import palettable

palettable.colorbrewer.sequential.Greys_5_r.colors = [[60,60,60],[105,105,105],[0,0,255],[255,255,0],[240,240,240]]
paleta = palettable.colorbrewer.sequential.Greys_5_r.mpl_colormap

def lisa_cluster(moran_loc, gdf, p=0.05, ax=None,
                 legend=True, legend_kwds=None, **kwargs):
...
 if ax is None:
        figsize = kwargs.pop('figsize', None)
        fig, ax = plt.subplots(1, figsize=figsize)
    else:
        fig = ax.get_figure()

    gdf.assign(cl=labels).plot(column='cl', categorical=True,
                               k=2, cmap=paleta, linewidth=0.1, ax=ax,
                               edgecolor='white', legend=legend,
                               legend_kwds=legend_kwds, **kwargs)
    ax.set_axis_off()
    ax.set_aspect('equal')
    return fig, ax

So I want that regions in each quadrant has the following collors:

1(HH)-Black
2(HL)-Dark gray
3(LL)-Yellow
4(LH) - Blue
Non significant - light gray

The problem is that the colors are MERGING and I do not know why. I labeld the regions with their repectvly quadrant to showenter image description here

2003 and 2004 are ok. At 2002 map, the colors yellow and blue ( and blue and light gray I think) merged


Solution

  • The solution is to change the object imported from the function mask_local_auto. The code is here.

    The colors are defined in the object colors5:

    (...)
    #create a mask for local spatial autocorrelation  
    cluster = moran_hot_cold_spots(moran_loc, p)
    
    cluster_labels = ['ns', 'HH', 'LH', 'LL', 'HL']
    labels = [cluster_labels[i] for i in cluster]
    
    colors5 = {0: 'lightgrey',
               1: '#d7191c',
               2: '#abd9e9',
               3: '#2c7bb6',
               4: '#fdae61'}
    colors = [colors5[i] for i in cluster]  # for Bokeh
    # for MPL, keeps colors even if clusters are missing:
    x = np.array(labels)
    y = np.unique(x)
    colors5_mpl = {'HH': '#d7191c',
                   'LH': '#abd9e9',
                   'LL': '#2c7bb6',
                   'HL': '#fdae61',
                   'ns': 'lightgrey'}
    colors5 = [colors5_mpl[i] for i in y]  # for mpl
    (...)