Plotly: how to use custom color palette

Viewed 43

I made a color palette, with colors correlated to temperature (measured in Kelvin). The code to convert temperature to rgb I found here: https://gist.github.com/petrklus/b1f427accdf7438606a6

  1. Show the color palette
  2. Capture [r,g,b] in colorList
step_size = 10
colorList=[]
# from 0 K to 15 K, convert K to rgb, plot and capture
for i in range(0, 15000, step_size):
    color = list(map(lambda div: div, convert_K_to_RGB(i)))
    colorList.append(color)
    plt.plot((i, i), (0, 1), linewidth=step_size/2.0, linestyle="-", color=color)

Color Palette

How to use this palette instead of a standard one in Plotly?

What I tried:

  1. Make a dict
dC = {'pallete':colorList}
  1. Make a pandas dataframe
dCpd = pd.DataFrame(dC)
  1. Extract RGB from pandas column
xRed = [dCpd['pallete'][i][0] for i in range(len(dCpd['pallete']))]
xGreen = [dCpd['pallete'][i][1] for i in range(len(dCpd['pallete']))]
xBlue = [dCpd['pallete'][i][2] for i in range(len(dCpd['pallete']))]
  1. Make a final dic to pass to plotly
xRGB = {'Red':xRed, 'Green':xGreen, 'Blue':xBlue}
  1. Plotly scatter 3D
fig = px.scatter_3d(datap,x = x, y = y, z = z, color=datap['B-V'],
                    color_continuous_scale= xRGB,
                    color_continuous_midpoint=0)
fig.show()

datap, is the pandas df I am trying to apply the color palette to. I am plotting the position of stars, and I want their color to represent the temperature. The logic is: star B-V color index column (datap[B-V]) -> compute Temperature-> compute RGB from Temperature-> apply that color

The error I get:

Invalid value of type 'builtins.dict' received for the 'colorscale' property of make_figure

plot example with a standard color palette in plotly that works, but doesn't represent color-temperature correlation I am trying to implement:

fig = px.scatter_3d(datap,x = x, y = y, z = z, color=datap['B-V'],
                     color_continuous_scale=px.colors.diverging.Spectral_r,
                     color_continuous_midpoint= 0,
                     hover_data=(['Name','magV']))
fig.show()

example

I want the B-V color palette to be as the example in the first png

1 Answers

This is as close as I could get, seems to be a good representation:

Find the blue-end and red-end of the palette

redEnd = f"rgb{tuple(dCpd['pallete'][0])}"
blueEnd = f"rgb{tuple(dCpd['pallete'].values[-1])}"

Check:

print(f"'{redEnd}'")
print(f"'{blueEnd}'")

Print:

'rgb(1.0, 0.2663545845364998, 0.0)'
'rgb(0.7100281129287552, 0.8043951737139605, 1.0)'

Pass the values to a continous color representation in plotly:

fig = px.scatter_3d(datap,x = x, y = y, z = z, color=datap['B-V'],

                       color_continuous_scale= [[0,blueEnd], 
                                                [0.566,'white'],
                                                [0.833,'orange'],
                                                [1,redEnd]],
                       color_continuous_midpoint=0.4759,
                        hover_data=(['Name','magV','Temperature']))

Result: Ignore the white star(that is our sun, for easy identification) and the 2 dark grey stars (no data for B-V and temperature computation) good plot

Related