plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Functions to map floats to RGB values

Open roek-pnnl opened this issue 8 months ago • 3 comments

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.

roek-pnnl avatar May 07 '25 03:05 roek-pnnl

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

gvwilson avatar May 08 '25 18:05 gvwilson

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?

roek-pnnl avatar May 08 '25 20:05 roek-pnnl

cc @LiamConnors for documentation

gvwilson avatar May 12 '25 13:05 gvwilson