Colorbar of a Contour Plot

Hello,

I have a matrix of discrete values, and I would like to plot it as a contour plot instead of a heatmap, because reasons. This works well enough, except that the colorbar is strange and I can’t find a way to correct it.

The MWE:

using Plots # gr
x = rand(1:10, 100, 100)
contour(x)

Contour
For my use case, a discrete colorbar with bins would be ideal (see here for reference). I would also be fine with a continuous colorbar, which is the default for heatmaps in GR. But instead I get this strange bar with discrete levels that do not correspond to the step size. Moreover, the bins are not colored in. Using contourf() or fill = true fills in the bins, but they still do not correspond to the step size.

I’ve tried playing around with the following attributes, without success:
levels, colorbar_formatter, colorbar_ticks, and colorbar_continous_values

Any help would be appreciated,
Thanks

Try this:

contourf(x, levels=1:10)
2 Likes

Thanks for the suggestion, it works!

Ideally, I would like to fill the colorbar without filling in the contour plot, but I don’t expect that is possible.

You can use the tired trick with layouts:

l = @layout [a{0.9w} b]
p1 = contour(x, levels=1:10, colorbar=false)
p2 = contourf(NaN*x, levels=1:10, framestyle=:none, legend=false, clims=extrema(x), colorbar=true)
plot(p1, p2, layout=l)
1 Like