2

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

Sundar R
  • 13,776
  • 6
  • 49
  • 76
Anton Degterev
  • 591
  • 2
  • 12

1 Answers1

0

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/

Bill
  • 5,600
  • 15
  • 27
  • Yes, it can be written like this. But this is not a solution to the problem. Although there is some appearance that the palette is discrete, it still remains continuous, as I have in the lower figure. Instead of signatures of discrete values, the entire continuous sequence of values is displayed on the palette scale – Anton Degterev Jul 11 '22 at 04:09
  • Are you trying to remove the numeric labels from the color scale? – Bill Jul 11 '22 at 05:06
  • I would like to remove all intermediate values on the scale and leave only those that are really present in the data. For the cleanest design, it is also desirable that the signature of each discrete class is in the middle of its color interval on the scale – Anton Degterev Jul 11 '22 at 12:24
  • You want to use colorbar_ticks but that is only supported in the pyplot backend at the moment. – Bill Jul 11 '22 at 22:53