Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing in cmap and norm for the odc.geo.xr.ODCExtension.explore function #137

Open
alexgleith opened this issue Apr 4, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@alexgleith
Copy link
Contributor

Hey folks

Not sure if this is expected to work, but I have a nice colourmap for a categorical plot, example here.

Code is something like this:

from matplotlib import colors

classes = [
    [1, "bare_land", "#968640"],
    [2, "forest", "#064a00"],
    [3, "crops", "#ffce33"],
    [4, "grassland", "#d7ffa0"],
    [5, "settlements", "#b3b2ae"],
    [6, "mangroves", "#07b28d"],
    [7, "water", "#71a8ff"],
    [8, "quarry", "#b03a2e"]
]

values_list = [c[0] for c in classes]
color_list = [c[2] for c in classes]

# Build a listed colormap.
c_map = colors.ListedColormap(color_list)
bounds = values_list + [9]
norm = colors.BoundaryNorm(bounds, c_map.N)

data["class"].isel(time=0).plot.imshow(cmap=c_map, norm=norm, size=10)

Would it be possible for the amazing explore() function to be able to render with these colourmaps? Currently it fails with TypeError: Object of type BoundaryNorm is not JSON serializable

@Kirill888
Copy link
Member

Kirill888 commented Apr 4, 2024

It's actually an issue with mapping function itself, it doesn't know of norm= so passes it on to folium/ipyleaflet, where it tries to push it to JavaScript, hence the error.

So these need to be augmented to understand norm= parameter:

def _matplotlib_colorize(x, cmap, vmin=None, vmax=None, nodata=None, robust=False):

odc-geo/odc/geo/_rgba.py

Lines 204 to 206 in 1f88854

def colorize(
x: Any,
cmap=None,

odc-geo/odc/geo/_map.py

Lines 54 to 65 in 1f88854

def add_to(
xx: Any,
map: Any,
*,
name: Optional[str] = None,
fmt: str = "png",
max_size: int = 4096,
resampling: str = "nearest",
# jpeg options:
transparent_pixel: Optional[Tuple[int, int, int]] = None,
# RGB conversion parameters
cmap: Optional[Any] = None,

@robbibt robbibt added the enhancement New feature or request label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants