Makie - control whether Colorbar for heatmap shows extended range

Hi,

When using heatmap together with Colorbar, I am having trouble understanding how to use the arguments colorrange and lowclip, highclip.

I want to plot an array with values ranging from 0 to 1. If I try to make a figure that only has colors corresponding to the range 0.2 to 0.8, I would expect to see extended triangles on the Colorbar above 0.8 and below 0.2. However, this does not happen by default.

# Define array of data to plot ranging from 0.0 to 1.0
z = rand(Float64, (10, 10));

# Plot only colors from 0.2 to 0.8
f, ax, hm = heatmap(z,colorrange=(0.2,0.8));
Colorbar(f[:,end+1],hm,ticks=(0.0:0.2:1.0,string.(0.0:0.2:1.0)));
f

I would expect the above example to include the extended triangles. What can I do to include them here?

Additionally:

  • How can I make heatmap and/or Colorbar determine automatically whether to show the triangles?
  • What can I pass to the arguments lowclip or highclip so that they impose the first and last color of a colormap object?

Thanks!

The colorbar is only a description of the colormap plus colorrange (and clip settings), not of the actual data. So the triangles will not appear or disappear depending on whether your data currently clips or not.
Currently they only appear if highclip / lowclip are set to some color, I think right now it might not even be possible to show them at all if they are the same color as the ends of the colormap. (This was a change from a couple months ago but I’m still not completely happy with that behavior)

I see. So, in the above example, I should set lowclip and highclip to the first and last colors. But is there a way to automatically allow heatmap to figure it out, i.e., lowclip = :auto or something similar? To me, this would be ideal default behavior.

I also noticed that contourf works slightly differently, which was also surprising. But it would be great to be able to interchange the two and obtain identical results. For example, it is possible with a contourf object to use the attributes extendhigh / extendlow, and while these appear to exist in a heatmap object, they are not used. I apologize that I am not versed enough in Julia coding to contribute any possible solutions, but I hope my comments help.

In trying to understand how I could make use of these functions, I came up with a few test cases that helped clarify these behaviors, perhaps they are useful for future development:

# Define a matrix of random numbers ranging from 0 to 1.
z = rand(Float64, (10, 10));

# Plot values ranging from 0.2 to 0.8.
# Triangles expected on both sides, no triangles appear.
f, ax, hm = heatmap(z,colorrange=(0.2,0.8));
cb = Colorbar(f[:,end+1],hm,ticks=(0.0:0.2:1.0,string.(0.0:0.2:1.0)));
f

# Plot values ranging from 0.2 to 1.0, and include a colormap.
# Triangles expected on bottom, triangles appear on both sides.
f, ax, hm = heatmap(z,colorrange=(0.2,1.0),colormap=cgrad(:Paired_5));
cb = Colorbar(f[:,end+1],hm,ticks=(0.0:0.2:1.0,string.(0.0:0.2:1.0)));
f

# Plot ranging from 0.2 to 1.0 using contourf, and include a colormap.
# Triangles expected on bottom, no triangles appear.
f, ax, hm = contourf(z,levels=0.2:0.2:1.0,colormap=cgrad(:Paired_5));
cb = Colorbar(f[:,end+1],hm,ticks=(0.0:0.2:1.0,string.(0.0:0.2:1.0)));
f

I’ll also add this as an issue in github, in case it helps.

contourf has exactly that, but the interface has grown a bit organically and hasn’t been consolidated across objects, yet. That’s definitely still needed.

For contourf the logic is that you can cut off data with the levels, but if you want to include all data beyond the levels, then you set extendhigh and extendlow and that in turn makes the triangles appear with the Colorbar.