I recently wanted to compare Viridis with Plasma for a plot and change the colormap to be white as the zero value.
Thought I'd clean this up and add support for easier handling of custom colormaps.
Instead of playing around with the JsonNode after converting to PlotJson (or serializing), one can either assign one of the predefined custom colormaps:
Plasma
Magma
Inferno
each with and without zero value as white
Viridis with zero as white
white to black instead of plotly's "Greys", which is black-to-white) by using the
trace.customCmap = getCustomMap(<field of PredefinedCustomMaps>)
or create a CustomColorMap object and assign it. The fields of it are rawColors, which are just a sequence of tuple[r, g, b: float] in the range of 0.0 .. 1.0. The custom maps can consist of N values. They will be taken to cover the whole data range.
The colormap range can additionally be restricted (or enlarged) via the zmin and zmax fields (only for heatmap, was not sure if I should add it for Countour?).
The normal colormap field has to be set to Custom for the custom map to be considered.
Alternatively the colormap sugar function can be used on a Plot[T] object. This is more convenient imo. One can hand one of:
ColorMap: enum: Plotly's default colormaps
PredefinedCustomMap: enum on of the predefined custom maps
CustomColorMap a custom colormap object
rawColors a sequence of raw colors as shown above
See the fig20 example and the test case for explicit usage.
I recently wanted to compare Viridis with Plasma for a plot and change the colormap to be white as the zero value. Thought I'd clean this up and add support for easier handling of custom colormaps.
Instead of playing around with the
JsonNode
after converting toPlotJson
(or serializing), one can either assign one of the predefined custom colormaps:each with and without zero value as white
or create a
CustomColorMap
object and assign it. The fields of it arerawColors
, which are just a sequence oftuple[r, g, b: float]
in the range of0.0 .. 1.0
. The custom maps can consist ofN
values. They will be taken to cover the whole data range. The colormap range can additionally be restricted (or enlarged) via thezmin
andzmax
fields (only for heatmap, was not sure if I should add it forCountour
?).The normal
colormap
field has to be set toCustom
for the custom map to be considered.Alternatively the
colormap
sugar function can be used on aPlot[T]
object. This is more convenient imo. One can hand one of:ColorMap: enum
: Plotly's default colormapsPredefinedCustomMap: enum
on of the predefined custom mapsCustomColorMap
a custom colormap objectrawColors
a sequence of raw colors as shown aboveSee the fig20 example and the test case for explicit usage.