Black Lives Matter. Please consider donating to Black Girls Code today.

Problem with shifting color maps

I need to be able to make a color map for several possible categories where not all the categories show up all the time, e.g.

sample_colormap = [[0.0, ‘black’],[0.2,‘black’],[0.2,‘yellow’],[0.4,‘yellow’],[0.4,‘green’],[0.6,‘green’],[0.6,‘red’],[0.8,‘red’],[0.8,‘blue’],[1,‘blue’]]

but I have noticed that if 0 is not in the dataset then all the other colors MOVE to compensate for that (i.e. if the smallest number is 0.2 suddenly 0.2’s are rendering BLACK rather than YELLOW). How do I turn this behavior OFF? I would also note that I have attempted putting x=np.nan y=np.nan color=0 into my dataset to discourage this behavior but that does not work.

Thanks!

Hi @bebuck, when you define a colorscale, the interval [0, 1] corresponds to the interval of colors and your interval of values is mapped onto this interval, using usually its min and max values for mapping to 0 or 1, or user-defined trace parameters such as cmin/cmax for markers in a go.Scatter trace, or zmin/zmax in a go.Heatmap trace. Take a look at the examples of https://plot.ly/python/colorscales/, for example https://plot.ly/python/colorscales/#colorscale-for-scatter-plots. So you need to set these parameters to say that you always want 0 to be mapped to the first color and 1 to the last one.

Thanks for the quick reply. I guess that’s getting me ideas about work arounds, but it sounds like what I have to do then is adjust the color scale to match the range of colors available. Part of what I’d like to do is change the number of colors active in the board – adding a new color – when the user clicks. I’ll see if putting the new color in the middle helps.

Hum, maybe I was not clear : the colorscale is defined between 0 and 1, but you can change which data values are mapped to 0 and 1, using parameters such as cmin, cmax (there is also cmid for the middle of the scale).