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

Adds function to sample colorscales #3136

Merged
merged 7 commits into from May 10, 2021

Conversation

CarlAndersson
Copy link
Contributor

@CarlAndersson CarlAndersson commented Apr 7, 2021

Addition to the colors module which can sample a colorscale at arbitrary or equally spaced points.
Solves #2484.

Code PR

  • I have read through the contributing notes and understand the structure of the package. In particular, if my PR modifies code of plotly.graph_objects, my modifications concern the codegen files and not generated files.
  • I have added tests (if submitting a new feature or correcting a bug) or
    modified existing tests.
  • For a new feature, I have added documentation examples in an existing or
    new tutorial notebook (please see the doc checklist as well).
  • I have added a CHANGELOG entry if fixing/changing/adding anything substantial.
  • For a new feature or a change in behaviour, I have updated the relevant docstrings in the code to describe the feature or behaviour (please see the doc checklist as well).

@nicolaskruchten
Copy link
Member

Thank you! We will be doing some work in this library in the coming weeks to prepare for the 5.0 release and I will take a closer look at this PR then :)

if colorscale in PLOTLY_SCALES:
colorscale = PLOTLY_SCALES[colorscale]
else:
raise exceptions.PlotlyError(
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand this error message... Under what circumstances would we accept "an rgb color or a hex color" here?

Interpolates between colors in a colorscale to find the specific colors
corresponding to the specified sample values. The colorscale can be specified
as a list of `[scale, color]` pairs, as a list of colors, or as a named
plotly colorscale. The samplepoints can be specefies an iterable of specific
Copy link
Member

Choose a reason for hiding this comment

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

typo here: "specefies an"

@nicolaskruchten
Copy link
Member

This is a pretty great little function that really reuses a bunch of built-in logic, thanks so much! I've left a couple of comments and if you could please merge master into this branch or rebase it on top of master, I think we can merge it in without too much fuss. It might be nice to add a test?

@nicolaskruchten
Copy link
Member

Note to self: this new function needs to be added to the __all__ in plotly.express.colors

A typo in the docstring and a confusing error message.
A simple test that checks the consistency of sampling a colorscale
on the actual definition points, as well as sampling a very simple
scale at simple points.
Had some issues with running black locally due to version problems.
Now resolved and formatted the test code accordingly :)
@nicolaskruchten
Copy link
Member

Thanks for the changes!

In addition to my comment here #3185 (comment) about further work that could be done in this PR, I also had the idea that it might be possible to dynamically attach this function to objects like plotly.colors.sequential.Plasma so that we could call it via plotly.colors.sequential.Plasma.sample(15).

@CarlAndersson
Copy link
Contributor Author

I would love to have colorscale sampling available with that kind of syntax! However, since the colorscales are stored as plain python lists, we cannot attach new attributes to them... This would mean that we have to wrap the colorscales in a new class, which would open the opportunity to attach a lot more functionality to a colorscale, and an overall more object oriented approach to the entire colors module.

An interesting project, but perhaps a bit too much for this PR...

@nicolaskruchten
Copy link
Member

An interesting project, but perhaps a bit too much for this PR...

Yep, just wanted to record it somewhere :)

@nicolaskruchten
Copy link
Member

OK so I've merged #3186. If you want to merge that into this PR and use it in the new function, I'll merge this in today :)

# Conflicts:
#	packages/python/plotly/_plotly_utils/colors/__init__.py
#	packages/python/plotly/plotly/tests/test_core/test_colors/test_colors.py
@nicolaskruchten
Copy link
Member

Thanks very much!

@nicolaskruchten nicolaskruchten merged commit 2a59bc0 into plotly:master May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants