Skip to content

Commit

Permalink
add reference test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Sep 12, 2023
1 parent aba34cb commit ff7acf5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
41 changes: 36 additions & 5 deletions ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ end
f = Figure(resolution = (800, 400))
ax1 = PolarAxis(f[1, 1], title = "No spine", spinevisible = false)
scatterlines!(ax1, range(0, 1, length=100), range(0, 10pi, length=100), color = 1:100)

ax2 = PolarAxis(f[1, 2], title = "Modified spine")
ax2.spinecolor[] = :red
ax2.spinestyle[] = :dash
ax2.spinewidth[] = 5
scatterlines!(ax2, range(0, 1, length=100), range(0, 10pi, length=100), color = 1:100)

f
end

Expand All @@ -166,9 +166,9 @@ end
@reference_test "PolarAxis decorations" begin
f = Figure(resolution = (400, 400), backgroundcolor = :black)
ax = PolarAxis(
f[1, 1],
f[1, 1],
backgroundcolor = :black,
rminorgridvisible = true, rminorgridcolor = :red,
rminorgridvisible = true, rminorgridcolor = :red,
rminorgridwidth = 1.0, rminorgridstyle = :dash,
thetaminorgridvisible = true, thetaminorgridcolor = :blue,
thetaminorgridwidth = 1.0, thetaminorgridstyle = :dash,
Expand All @@ -179,7 +179,7 @@ end
thetaticklabelsize = 18, thetaticklabelcolor = :blue,
thetaticklabelstrokewidth = 1, thetaticklabelstrokecolor = :white,
)

f
end

Expand All @@ -192,3 +192,34 @@ end
end
f
end

@reference_test "Colorbar for recipes" begin
fig, ax, pl = barplot(1:3; color=1:3, colormap=Makie.Categorical(:viridis))
Colorbar(fig[1, 2], pl; size=100)
x = LinRange(-1, 1, 20)
y = LinRange(-1, 1, 20)
z = LinRange(-1, 1, 20)
values = [sin(x[i]) * cos(y[j]) * sin(z[k]) for i in 1:20, j in 1:20, k in 1:20]

# TO not make this fail in CairoMakie, we dont actually plot the volume
_f, ax, cp = contour(x, y, z, values; levels=10, colormap=:viridis)
Colorbar(fig[2, :], cp; size=300)

# horizontal colorbars
Colorbar(fig[1, 3][2, 1]; limits=(0, 10), colormap=:viridis,
vertical=false)
Colorbar(fig[1, 3][3, 1]; limits=(0, 5), size=25,
colormap=cgrad(:Spectral, 5; categorical=true), vertical=false)
Colorbar(fig[1, 3][4, 1]; limits=(-1, 1), colormap=:heat, vertical=false, flipaxis=false,
highclip=:cyan, lowclip=:red)

ax, hm = contourf(fig[2, 3][1, 1], xs, ys, zs;
colormap=:Spectral, levels=[-1, -0.5, -0.25, 0, 0.25, 0.5, 1])
Colorbar(fig[2, 3][1, 2], hm; ticks=-1:0.25:1)

ax, hm = contourf(fig[3, :][1, 1], xs, ys, zs;
colormap=:Spectral, colorscale=sqrt, levels=[ 0, 0.25, 0.5, 1])
Colorbar(fig[3, :][1, 2], hm; width=200)

fig
end
7 changes: 4 additions & 3 deletions src/makielayout/blocks/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function initialize_block!(cb::Colorbar)
if cb.colormap[] isa ColorMapping
cmap = cb.colormap[]
else
# Old way without Colormapping. We keep it, to be able to create a colormap directly
limits = lift(blockscene, cb.limits, cb.colorrange) do limits, colorrange
if all(!isnothing, (limits, colorrange))
error("Both colorrange + limits are set, please only set one, they're aliases. colorrange: $(colorrange), limits: $(limits)")
Expand All @@ -141,7 +142,7 @@ function initialize_block!(cb::Colorbar)
colors = lift(blockscene, cmap.mapping, cmap.color_mapping_type, cmap.color, limits, cb.nsteps; ignore_equal_values=true) do mapping, mapping_type, values, limits, n
if mapping === nothing
if mapping_type === Makie.banded
return sort!(convert(Vector{Float64}, mapping))
error("Banded without a mapping is invalid. Please use colormap=cgrad(...; categorical=true)")
elseif mapping_type === Makie.categorical
return convert(Vector{Float64},1:length(unique(values)))
else
Expand Down Expand Up @@ -197,7 +198,6 @@ function initialize_block!(cb::Colorbar)

# for continous colormaps we sample a 1d image
# to avoid white lines when rendering vector graphics

continous_pixels = lift(blockscene, cb.vertical, colors,
cmap.color_mapping_type) do v, colors, mapping_type
if mapping_type !== Makie.categorical
Expand All @@ -206,9 +206,9 @@ function initialize_block!(cb::Colorbar)
n = length(colors)
return v ? reshape((colors), 1, n) : reshape((colors), n, 1)
end

# TODO, implement interpolate = true for irregular grics in CairoMakie
# Then, we can just use heatmap! and don't need the image plot!

show_cats = Observable(false; ignore_equal_values=true)
show_continous = Observable(false; ignore_equal_values=true)
on(blockscene, cmap.color_mapping_type; update=true) do type
Expand Down Expand Up @@ -328,6 +328,7 @@ function initialize_block!(cb::Colorbar)

ticks = Observable{Any}()
map!(ticks, colors, cmap.color_mapping_type, cb.ticks) do cs, type, ticks
# For categorical we just enumerate
type === Makie.categorical ? (1:length(cs), string.(cs)) : ticks
end

Expand Down

0 comments on commit ff7acf5

Please sign in to comment.