Skip to content

Functions to map floats to RGB values #5168

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

Open
ghproek opened this issue May 7, 2025 · 2 comments
Open

Functions to map floats to RGB values #5168

ghproek opened this issue May 7, 2025 · 2 comments
Labels
feature something new P3 backlog

Comments

@ghproek
Copy link

ghproek commented May 7, 2025

It would be useful if Plotly exposed functions to map floats, e.g. values in [0,1], to corresponding RGB strings formatted as 'rgb(255, 0, 0)'. There are lots of use cases for this. For example, if you want to plot a network where each edge is colored according to a weight between 0 and 1 using a continuous color scale. The only way I know to do this right now is to make a separate trace for each edge in the graph and specify its color manually.

@gvwilson gvwilson added feature something new P3 backlog labels May 8, 2025
@gvwilson
Copy link
Contributor

gvwilson commented May 8, 2025

thanks for the suggestion @ghproek - we're unlikely to get to this any time soon, but I'd be happy to prioritize review of a community PR. thanks - @gvwilson

@ghproek
Copy link
Author

ghproek commented May 8, 2025

In fact I just discovered that this function already exists!

Input:

import plotly

colorscale = plotly.colors.get_colorscale('Bluered') 
print(plotly.colors.sample_colorscale(colorscale, 0.5))
print(plotly.colors.sample_colorscale(colorscale, [0.1, 0.2]))

Output:

['rgb(128, 0, 128)']
['rgb(26, 0, 230)', 'rgb(51, 0, 204)']

I've used Plotly for years and years and never found this in the documentation. What would be really helpful is if this function were explicitly demoed in the main pages you hit when you search for color scales, e.g. https://plotly.com/python/builtin-colorscales. Would that be at all feasible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P3 backlog
Projects
None yet
Development

No branches or pull requests

2 participants