Hi msuths1,
Plotly does not provide the colorscale attribute for lines of a scatter trace, of mode='lines'
.
Even heatmap, contour and surface donβt return the color code in a colorscale corresponding to a z-value.
For your vectors you can use a custom function that returns the color for each particular value in an interval.
To call such a function you have first to choose from plotly.colors https://github.com/plotly/plotly.py/tree/master/packages/python/plotly/_plotly_utils/colors
a list of colors, pl_colors
, for a colorscale:
from plotly.colors import *
pl_colors = cmocean.deep # define the deep colorsale
The custom function:
from ast import literal_eval
import numpy as np
def get_color_for_val(val, vmin, vmax, pl_colors):
if pl_colors[0][:3] != 'rgb':
raise ValueError('This function works only with Plotly rgb-colorscales')
if vmin >= vmax:
raise ValueError('vmin should be < vmax')
scale = [k/(len(pl_colors)-1) for k in range(len(pl_colors))]
colors_01 = np.array([literal_eval(color[3:]) for color in pl_colors])/255. #color codes in [0,1]
v= (val - vmin) / (vmax - vmin) # val is mapped to v in [0,1]
#find two consecutive values in plotly_scale such that v is in the corresponding interval
idx = 1
while(v > scale[idx]):
idx += 1
vv = (v - scale[idx-1]) / (scale[idx] -scale[idx-1] )
#get [0,1]-valued color code representing the rgb color corresponding to val
val_color01 = colors_01[idx-1] + vv * (colors_01[idx ] - colors_01[idx-1])
val_color_0255 = (255*val_color01+0.5).astype(int)
return f'rgb{str(tuple(val_color_0255))}'
Example:
get_color_for_val(2.3, 1, 5, pl_colors)
returns 'rgb(62, 81, 157)'
In your case vmin, vmax are the min, respectively the max value in the column cos2
.
Unfortunately you cannot plot a naturally associated colorbar. To get it displayed you should define a dummy Scatter
trace, mode='markers'
, with two markers, of size= 0, and marker['color']=[vmin, vmax]
.