AlgebraOfGraphics.jl icon indicating copy to clipboard operation
AlgebraOfGraphics.jl copied to clipboard

Colorbar shows wrong values when using `Contourf`, with `colorrange` or `levels`

Open laikq opened this issue 1 year ago • 0 comments

Bug description

Using visual(Contourf; colorrange=(x, y)) will result in an incorrect plot where the colorbar does not reflect the values being plotted. This is apparently because setting colorrange does not actually affect the Contourf plot, and just sets the colorbar boundaries. Setting levels instead of colorrange will give the expected colors, but does not affect the colorbar.

Workaround: Use visual(Contourf; colorrange=(x, y), levels=range(x, y)) simultaneously.

Steps to reproduce

Setup code:

x = range(-2π, 2π, 30)
dat = [(x, y, sin(x+y)) for (x, y) in Iterators.product(x, x)] |> vec
df = DataFrame(dat, [:x, :y, :val])

Correct behavior using visual(Heatmap; colorrange) for comparison:

data(df) * mapping(:x, :y, :val) * visual(Heatmap; colormap=:curl, colorrange=(-1, 3)) |> draw

Using Heatmap, the crests of the sine are correctly displayed in white. The colorbar correctly identifies white as the value 1

Incorrect behavior using visual(Contourf; colorrange):

data(df) * mapping(:x, :y, :val) * visual(Contourf; colormap=:curl, colorrange=(-1, 3)) |> draw

Using Contourf with colorrange, the crests of the sine are displayed as dark red, which would correspond to a value of 3 according to the colorbar. This is incorrect

(A bit less, but still) incorrect behavior using visual(Contourf; levels):

data(df) * mapping(:x, :y, :val) * visual(Contourf; colormap=:curl, levels=-1:0.4:3) |> draw

Using Contourf with levels, the crests of the sine are now correctly displayed as white. However, the colorbar incorrectly identifies white with the value 0

Version info

  • Julia Version: 1.9.3 (commit bed2cd540a)
  • AlgebraOfGraphics Version: 0.6.18
  • CairoMakie Version: 0.11.9

(edit: Tested with the newest AlgebraOfGraphics & CairoMakie — Apologies for not doing that from the start!)

laikq avatar Feb 27 '24 13:02 laikq