Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
jk/colorbar fixes (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel authored Apr 10, 2021
1 parent e7d673c commit 9031bdf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/makielayout/colorbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Colorbar(fig[4, 1], height = 25, limits = (-1, 1), colormap = :heat,
fig
```

You can also automatically choose colormap and limits for certain plot objects by passing them as the second argument.
If you pass a `plotobject`, a `heatmap` or `contourf`, the Colorbar is set up automatically such that it tracks these objects' relevant attributes like `colormap`, `colorrange`, `highclip` and `lowclip`. If you want to adjust these attributes afterwards, change them in the plot object, otherwise the Colorbar and the plot object will go out of sync.

```@example
using CairoMakie
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ $(ATTRIBUTES)
Attributes(;
default_theme(scene)...,
colormap = [:black, :white],
colorrange = automatic,
interpolate = true,
fxaa = false,
lowclip = nothing,
Expand All @@ -70,6 +71,7 @@ $(ATTRIBUTES)
Attributes(;
default_theme(scene)...,
colormap = :viridis,
colorrange = automatic,
linewidth = 0.0,
interpolate = false,
levels = 1,
Expand Down Expand Up @@ -120,6 +122,7 @@ $(ATTRIBUTES)
default_theme(scene)...,
color = nothing,
colormap = :viridis,
colorrange = automatic,
shading = true,
fxaa = true,
lowclip = nothing,
Expand Down Expand Up @@ -147,6 +150,7 @@ $(ATTRIBUTES)
linewidth = 1.0,
color = :black,
colormap = :viridis,
colorrange = automatic,
linestyle = nothing,
fxaa = false
)
Expand Down Expand Up @@ -183,6 +187,7 @@ $(ATTRIBUTES)
default_theme(scene)...,
color = :black,
colormap = :viridis,
colorrange = automatic,
interpolate = false,
shading = true,
fxaa = true,
Expand All @@ -204,6 +209,7 @@ $(ATTRIBUTES)
default_theme(scene)...,
color = :gray65,
colormap = :viridis,
colorrange = automatic,
marker = Circle,
markersize = 10,

Expand Down
7 changes: 7 additions & 0 deletions src/makielayout/defaultattributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ function default_attributes(::Type{Colorbar}, scene)
end

@doc """
Colorbar(parent; kwargs...)
Colorbar(parent, plotobject; kwargs...)
Colorbar(parent, heatmap::Heatmap; kwargs...)
Colorbar(parent, contourf::Contourf; kwargs...)
Add a Colorbar to `parent`. If you pass a `plotobject`, a `heatmap` or `contourf`, the Colorbar is set up automatically such that it tracks these objects' relevant attributes like `colormap`, `colorrange`, `highclip` and `lowclip`. If you want to adjust these attributes afterwards, change them in the plot object, otherwise the Colorbar and the plot object will go out of sync.
Colorbar has the following attributes:
$(let
Expand Down
28 changes: 23 additions & 5 deletions src/makielayout/layoutables/colorbar.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function layoutable(::Type{<:Colorbar}, fig_or_scene, plot::AbstractPlot; kwargs...)

for key in (:colormap, :limits)
if key in keys(kwargs)
error("You should not pass the `$key` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.")
end
end

layoutable(Colorbar, fig_or_scene;
colormap = plot.colormap,
limits = plot.colorrange,
Expand All @@ -10,6 +16,12 @@ end

function layoutable(::Type{<:Colorbar}, fig_or_scene, heatmap::Union{Heatmap, Image}; kwargs...)

for key in (:colormap, :limits, :highclip, :lowclip)
if key in keys(kwargs)
error("You should not pass the `$key` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.")
end
end

layoutable(Colorbar, fig_or_scene;
colormap = heatmap.colormap,
limits = heatmap.colorrange,
Expand All @@ -19,19 +31,25 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene, heatmap::Union{Heatmap, Im
)
end

function layoutable(::Type{<:Colorbar}, fig_or_scene, plot::AbstractPlotting.Contourf; kwargs...)
function layoutable(::Type{<:Colorbar}, fig_or_scene, contourf::AbstractPlotting.Contourf; kwargs...)

for key in (:colormap, :limits, :highclip, :lowclip)
if key in keys(kwargs)
error("You should not pass the `$key` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.")
end
end

steps = plot._computed_levels
steps = contourf._computed_levels

limits = lift(steps) do steps
steps[1], steps[end]
end

layoutable(Colorbar, fig_or_scene;
colormap = plot._computed_colormap,
colormap = contourf._computed_colormap,
limits = limits,
lowclip = plot._computed_extendlow,
highclip = plot._computed_extendhigh,
lowclip = contourf._computed_extendlow,
highclip = contourf._computed_extendhigh,
kwargs...
)

Expand Down
16 changes: 16 additions & 0 deletions test/unit_tests/makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ end
@test ax.targetlimits[] == BBox(-5, 11, 5, 7)
@test ax.finallimits[] == BBox(-5, 11, 5, 7)
end

@testset "Colorbar plot object kwarg clash" begin
for attr in (:colormap, :limits)
f, ax, p = scatter(1:10, 1:10, color = 1:10, colorrange = (1, 10))
Colorbar(f[2, 1], p)
@test_throws ErrorException Colorbar(f[2, 1], p; Dict(attr => nothing)...)
end

for attr in (:colormap, :limits, :highclip, :lowclip)
for F in (heatmap, contourf)
f, ax, p = F(1:10, 1:10, randn(10, 10))
Colorbar(f[1, 2], p)
@test_throws ErrorException Colorbar(f[1, 3], p; Dict(attr => nothing)...)
end
end
end

0 comments on commit 9031bdf

Please sign in to comment.