Visitar URL original
Add _repr_png_ and _repr_html_ to Colormap objects. by bdice · Pull Request #17888 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20210506230747/https://github.com/matplotlib/matplotlib/pull/17888
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

Add _repr_png_ and _repr_html_ to Colormap objects. #17888

Merged
merged 7 commits into from Jul 12, 2020

Conversation

@bdice
Copy link
Contributor

@bdice bdice commented Jul 11, 2020 •

PR Summary

This PR adds _repr_png_ and _repr_html_ methods to the matplotlib.colors.Colormap class, enabling visual representations of Colormap objects in IPython / Jupyter settings.

Demo

image

Resolves #15616.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/next_api_changes/* if API changed in a backward-incompatible way
@bdice bdice marked this pull request as ready for review Jul 11, 2020
bdice added 2 commits Jul 11, 2020
Copy link
Contributor

@dopplershift dopplershift left a comment

Looks good pending CI passing

@dopplershift dopplershift added this to the v3.4.0 milestone Jul 11, 2020
@dopplershift
Copy link
Contributor

@dopplershift dopplershift commented Jul 11, 2020 •

This is going to be really awesome for our notebook users. I'll be sure to feature it in the workshops I teach.

Copy link
Member

@tacaswell tacaswell left a comment

Better tests are always better, but I'm also 👍 in with any level of testing.

@QuLogic
Copy link
Member

@QuLogic QuLogic commented Jul 11, 2020

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.

@jklymak
Copy link
Contributor

@jklymak jklymak commented Jul 11, 2020

... good point - extend='both' would be a nice addition....

@tacaswell
Copy link
Member

@tacaswell tacaswell commented Jul 12, 2020

I am happy to bank this improvement and then sort out how to handle the over / under / bad.

@story645 suggested something like f'{name} (over {html rect of over color} | under {html rect of under color} | bad {html rect of bad color}'

@tacaswell
Copy link
Member

@tacaswell tacaswell commented Jul 12, 2020

Spoke with @bdice on audio. Because we don't currently have public api to get the under/over/bad values I am going to merge this PR as-is and @bdice we open a new PR adding get_* methods and adding them to the html repr.

@jklymak
Copy link
Contributor

@jklymak jklymak commented Jul 12, 2020

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.

@tacaswell
Copy link
Member

@tacaswell tacaswell commented Jul 12, 2020

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.

@tacaswell tacaswell merged commit 068e0f4 into matplotlib:master Jul 12, 2020
22 checks passed
22 checks passed
@github-actions
flake8
Details
@github-actions
eslint
Details
@lgtm-com
LGTM analysis: JavaScript No code changes detected
Details
@lgtm-com
LGTM analysis: Python 1 new alert
Details
@travis-ci
Travis CI - Pull Request Build Passed
Details
ci/circleci: docs-python37 Your tests passed on CircleCI!
Details
@github-actions
ci/circleci: docs-python37 artifact Link to 0/doc/build/html/index.html
Details
ci/circleci: docs-python38 Your tests passed on CircleCI!
Details
@github-actions
ci/circleci: docs-python38 artifact Link to 0/doc/build/html/index.html
Details
ci/circleci: docs-python38-min Your tests passed on CircleCI!
Details
@codecov
codecov/patch 100.00% of diff hit (target 50.00%)
Details
@codecov
codecov/project/library 76.78% (target 50.00%)
Details
@codecov
codecov/project/tests 99.30% (+0.02%) compared to ec0132f
Details
@appveyor
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
@azure-pipelines
matplotlib.matplotlib Build #20200712.15 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job Linux_py37) Job Linux_py37 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job Linux_py38) Job Linux_py38 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job Windows_py37) Job Windows_py37 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job Windows_py38) Job Windows_py38 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job Windows_pyPre) Job Windows_pyPre succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job macOS_py37) Job macOS_py37 succeeded
Details
@azure-pipelines
matplotlib.matplotlib (Job macOS_py38) Job macOS_py38 succeeded
Details
@tacaswell
Copy link
Member

@tacaswell tacaswell commented Jul 12, 2020

Congratulations on your first Matplotlib PR @bdice 🎉

@bdice bdice deleted the bdice:colormap-repr branch Jul 12, 2020
@story645 story645 mentioned this pull request Sep 16, 2020
0 of 7 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

6 participants