Add _repr_png_ and _repr_html_ to Colormap objects. #17888
Conversation
|
Looks good pending CI passing |
|
This is going to be really awesome for our notebook users. I'll be sure to feature it in the workshops I teach. |
|
Better tests are always better, but I'm also |
|
For the PNG, I guess this is okay. However, colormaps have some other options like bad/over/under colors, which I think we could also insert into the HTML repr. |
|
... good point - extend='both' would be a nice addition.... |
|
I am happy to bank this improvement and then sort out how to handle the over / under / bad. @story645 suggested something like |
|
I guess I'm somewhat leery about this. Is there some reason this doesn't just create a colorbar and then fill the png with that? You won't get the over under w/o the colorbar logic anyhow. It would also be nice if the repr had an axes either going from 0-1 or from 0-N where N is the number of colours. |
|
I suggested to go down this route of direct PNG generation to get the simplest possible code to get a visual representation of the colormap. We already have a hard dependency on pillow so this seems way simpler than going through making a figure -> colorbar -> save it. I also think the split here between the png repr (just the color ramp) and the html (colorramp + metadata) so it will play nicely with some lower-fidelity UI (like terminals that can display png). I do not think that the tick labels are particularly useful (we don't show them in https://matplotlib.org/3.2.2/tutorials/colors/colormaps.html) because we only want to show the gamut. As for adding information to the html repr we should move that discussion to #17900 I'm going to go ahead and merge this (despite @jklymak 's concerns) because this is definitely an improvement and all of the user feedback I have seen on this has been super positive. |
068e0f4
into
matplotlib:master
|
Congratulations on your first Matplotlib PR @bdice |


PR Summary
This PR adds
_repr_png_and_repr_html_methods to thematplotlib.colors.Colormapclass, enabling visual representations ofColormapobjects in IPython / Jupyter settings.Demo
Resolves #15616.
PR Checklist