Skip to content

Allow for a custom scale/breaks in [legendgram] #255

Open
@lenkahas

Description

@lenkahas

It would be nice to also be able to use a custom scale for legendgram, in situations where the full colorbar/scale needs to be presented as well as how the data fits into it.

See example, which uses the legendgram function where pal.number is pal.n discussed in issue pysal/legendgram#12

import pandas as pd
import numpy as np
import mapclassify as mf
from matplotlib.colors import ListedColormap
import geopandas as gpd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.DataFrame(np.random.gamma(5,5,300), columns = ['var']) # create some random data
data['var'] = (data['var']/100)-0.12 # shift is left and on small scale so it it is the same as my situation
data = data.iloc[0:265,:] # make it uneven
data.hist();

nybb_path = gpd.datasets.get_path('nybb') # get some random spatial data
boros = gpd.read_file(nybb_path)
boros.set_index('BoroCode', inplace=True)

brk = np.arange(-0.9, 0.9,0.06) # define the custom breaks
b = ListedColormap(sns.color_palette("bwr", 30).as_hex()) # get the pallette
scheme = mf.UserDefined(data['var'], bins=brk) # define the bins

fig, ax = plt.subplots(1, figsize=(10, 10))
boros.plot(column = 'Shape_Area', cmap = b, ax = ax)
ax.axis('off')
legendgram(fig,ax, data['var'], breaks = scheme.bins, pal=b, bins = 50, legend_size=(0.4,0.13), # legend size in fractions of the axis loc = 'lower right');


IndexError Traceback (most recent call last)
in
10 bins = 50,
11 legend_size=(0.4,0.13), # legend size in fractions of the axis
---> 12 loc = 'lower right');

in llegendgram(f, ax, y, breaks, pal, bins, clip, loc, legend_size, frameon, tick_params)
46 for c in range(k):
47 for b in range(bucket_breaks[c], bucket_breaks[c+1]):
---> 48 patches[b].set_facecolor(pl(c/k))
49 #---
50 if clip is not None:

IndexError: list index out of range

image

Maybe the solution could look a little like this

image

Activity

jGaboardi

jGaboardi commented on Jun 14, 2025

@jGaboardi
Member
transferred this issue frompysal/legendgramon Jun 14, 2025
changed the title [-]Allow for a custom scale/breaks in legendgram [/-] [+]Allow for a custom scale/breaks in [legendgram][/+] on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jGaboardi@lenkahas

        Issue actions

          Allow for a custom scale/breaks in [legendgram] · Issue #255 · pysal/mapclassify