Adding "invalid" values to a heatmap

I am creating simple 2D heatmaps using the Python API. For some of my fields, I don’t have any data. This is OK and expected, but I would like to be able to mark these fields in the heatmap, for example by making those fields grey or black or any other easily recognizable color without affecting the rest of the color spectrum.

Currently, I just insert “-1” into those fields because my real data is always >=0, but of course plotly just interprets -1 as a regular value and thus just colors those fields blue.

Is there a way to insert “special” values into the heatmap for fields that should not be colored within the normal heatmap color spectrum? Or do you have another tip for how I could visually mark those fields?

Thank you!


Okay, so I just had the idea of just inserting NaN or Inf as invalid values. This is pretty much what I wanted, it makes those fields white. However, something like black or green would be even better for the visualisation.

So, as a follow-up question: Is there a way to dictate what color plotly uses to display NaN or Inf values?

There’s no native way to change the color mapping for NaN values. But, you can easily archive the same result by adding a custom NaN heatmap layer.

In brief, add a trace to your graph with z matrix consisting of 1s at the coordinates of the NaNs in your original z matrix. By defining, a custom trace colorscale, you can then assign a color to the NaNs values in your data.

See graph:

and python code: