What is the right way to make a colorbar for discrete data in Plots.jl?

Viewed 117

Let's say we have some discrete parameter, according to which we want to make a coloring on the plot using Plots.jl with GR backend. The parameter type (in the example below, it is Int64) clearly indicates that it is discrete, but if we do not take any additional actions, a continuous color scale will be applied to it in colorbar. If we explicitly specify the number of color gradations corresponding to the number of discrete classes, or at least just explicitly set the palette, then in this case the intermediate color gradations will disappear, but the scale will essentially remain continuous. What is the right way to make a colorbar for discrete data, where each class will correspond to a separate color and the value of that class?

x = (1:1:7); y = (1:1:7); z = [0, 1, 2, 2, 1, 0, 0]
plot(x, y, zcolor = z, seriestype = :scatter, 
markersize=5, label = "points")

result:

enter image description here

x = (1:1:7); y = (1:1:7); z = [0, 1, 2, 2, 1, 0, 0]
plot(x, y, zcolor = z, seriestype = :scatter, 
markersize=5, label = "points", color=palette(:heat, 3))

or

x = (1:1:7); y = (1:1:7); z = [0, 1, 2, 2, 1, 0, 0]
plot(x, y, zcolor = z, seriestype = :scatter, 
markersize=5, label = "points", color=palette(:heat))

result:

enter image description here

1 Answers

To genereralize, when making z a discrete color, you should use

 color = palette(:mypalettename, length(unique(z)))

as in

plot(x, y, zcolor = z, seriestype = :scatter,
   markersize=5, label = "points",
   color = palette(:heat, length(unique(z))),
   colorbar_ticks = [0.3333, 1.0, 1.6667])

but as of the current Plots version I think colorbar_ticks is only supported in pyplot() -- see https://docs.juliaplots.org/latest/generated/supported/

Related