Skip to content

Conversation

trygvrad
Copy link
Contributor

@trygvrad trygvrad commented Sep 3, 2025

This PR continues the work of #28658 and #28454, #29876, aiming to close #14168. (Feature request: Bivariate colormapping)

This PR allows Colorizer and ColorizingArtist to work with MultiNorm and BivarColormap and MultivarColormap

i.e. this PR will allow:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
a = np.arange(12).reshape(3,4)
b = np.arange(12).reshape(4,3).T

norm = mpl.colors.Normalize(vmin=0, vmax=11)
col = mpl.colorizer.Colorizer(cmap='BiOrangeBlue', norm=[norm, norm])

plt.imshow(col.to_rgba((a, b)))
image

Features not included in this PR:

  • Exposes the functionality provided by MultiNorm together with BivarColormap and MultivarColormap to the plotting functions axes.imshow(...), axes.pcolor(...), and axes.pcolormesh(...)`
  • fig.colorbar() equivalents for BivarColormap and MultivarColormap
  • Examples in the docs

PR checklist

  • new and changed code is tested

Comment on lines 2101 to 2102
data = [[0, 0], [1, 1]]
mdata = mcolorizer._ensure_multivariate_data(data, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 2x2, which makes it unclear which dimension you are checking or if it's checking the right one. Same for all below. Better not to be square.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 changed to data = [[0, 0, 0], [1, 1, 1]]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the ones below?

@trygvrad
Copy link
Contributor Author

trygvrad commented Sep 4, 2025

Thank you for the feedback @QuLogic and apologies for my sloppy mistakes :)

improved testing for colorizer+multinorm

Apply suggestions from code review

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>

updates based on feedback from @QuLogic
Comment on lines +831 to +832

return cm.colormaps[cmap_name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still can't run after the raise.

if isinstance(cmap, types):
return cmap

cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's mpl._val_or_rc.

Comment on lines 2101 to 2102
data = [[0, 0], [1, 1]]
mdata = mcolorizer._ensure_multivariate_data(data, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the ones below?

Comment on lines +224 to +227
if not in_init and self.norm.n_components != cmap_obj.n_variates:
raise ValueError(f"The colormap {cmap} does not support "
f"{self.norm.n_components} variates as required by "
f"the {type(self.norm)} on this Colorizer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block is over indented.

dt = np.dtype('float64, float64')
else:
dt = np.dtype('float32, float32')
reconstructed = np.ma.frombuffer(data.data, dtype=dt).reshape(data.shape)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a view to get this, instead of poking at the internal data buffer?

Copy link
Contributor Author

@trygvrad trygvrad Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, the following appears to work for regular arrays, but nor masked arrays

            reconstructed = data.view(dt)

I'll see if I can get a solution with view working for masked arrays as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Feature request: Bivariate colormapping
2 participants