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

Commit

Permalink
jk/minor ticks (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel authored Jan 28, 2021
1 parent 5f55ef0 commit aa9b4e7
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 31 deletions.
38 changes: 37 additions & 1 deletion docs/src/makielayout/laxis.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ nothing # hide

![axis ticks](example_axis_ticks.svg)

## Minor Ticks and Grids

You can show minor ticks and grids by setting `x/yminorticksvisible = true` and `x/yminorgridvisible = true` which are off by default.
You can set size, color, width, align etc. like for the normal ticks, but there are no labels.
The `x/yminorticks` attributes control how minor ticks are computed given major ticks and axis limits.
For that purpose you can create your own minortick type and overload `MakieLayout.get_minor_tickvalues(minorticks, tickvalues, vmin, vmax)`.

The default minor tick type is `IntervalsBetween(n, mirror = true)` where `n` gives the number of intervals each gap between major ticks is divided into with minor ticks, and `mirror` decides if outside of the major ticks there are more minor ticks with the same intervals as the adjacent gaps.

```@example
using CairoMakie
theme = Attributes(
Axis = (
xminorticksvisible = true,
yminorticksvisible = true,
xminorgridvisible = true,
yminorgridvisible = true,
)
)
fig = with_theme(theme) do
fig = Figure(resolution = (800, 800))
axs = [Axis(fig[fldmod1(n, 2)...],
title = "IntervalsBetween($(n+1))",
xminorticks = IntervalsBetween(n+1),
yminorticks = IntervalsBetween(n+1)) for n in 1:4]
fig
end
save("example_minor_ticks.svg", fig) # hide
nothing # hide
```

![minor ticks](example_minor_ticks.svg)


## Hiding Axis Spines and Decorations

You can hide all axis elements manually, by setting their specific visibility attributes to `false`, like
Expand All @@ -186,7 +222,7 @@ nothing # hide
![axis hide spines](example_axis_hidespines.svg)

To hide decorations, you can use `hidedecorations!`, or the specific `hidexdecorations!` and `hideydecorations!`.
When hiding, you can set `label = false`, `ticklabels = false`, `ticks = false` or `grid = false` as keyword
When hiding, you can set `label = false`, `ticklabels = false`, `ticks = false`, `grid = false`, `minorgrid = false` or `minorticks = false` as keyword
arguments if you want to keep those elements.
It's common, e.g., to hide everything but the grid lines in facet plots.

Expand Down
2 changes: 1 addition & 1 deletion src/makielayout/MakieLayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export Textbox
export linkxaxes!, linkyaxes!, linkaxes!
export AxisAspect, DataAspect
export autolimits!, limits!
export LinearTicks, WilkinsonTicks, MultiplesTicks
export LinearTicks, WilkinsonTicks, MultiplesTicks, IntervalsBetween
export hidexdecorations!, hideydecorations!, hidedecorations!, hidespines!
export tight_xticklabel_spacing!, tight_yticklabel_spacing!, tight_ticklabel_spacing!, tightlimits!
export layoutscene
Expand Down
68 changes: 63 additions & 5 deletions src/makielayout/defaultattributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,29 @@ function default_attributes(::Type{Axis}, scene)
"The width of the y grid lines."
ygridwidth = 1f0
"The color of the x grid lines."
xgridcolor = RGBAf0(0, 0, 0, 0.1)
xgridcolor = RGBAf0(0, 0, 0, 0.12)
"The color of the y grid lines."
ygridcolor = RGBAf0(0, 0, 0, 0.1)
ygridcolor = RGBAf0(0, 0, 0, 0.12)
"The linestyle of the x grid lines."
xgridstyle = nothing
"The linestyle of the y grid lines."
ygridstyle = nothing
"Controls if the x minor grid lines are visible."
xminorgridvisible = false
"Controls if the y minor grid lines are visible."
yminorgridvisible = false
"The width of the x minor grid lines."
xminorgridwidth = 1f0
"The width of the y minor grid lines."
yminorgridwidth = 1f0
"The color of the x minor grid lines."
xminorgridcolor = RGBAf0(0, 0, 0, 0.05)
"The color of the y minor grid lines."
yminorgridcolor = RGBAf0(0, 0, 0, 0.05)
"The linestyle of the x minor grid lines."
xminorgridstyle = nothing
"The linestyle of the y minor grid lines."
yminorgridstyle = nothing
"Controls if the bottom axis spine is visible."
bottomspinevisible = true
"Controls if the left axis spine is visible."
Expand Down Expand Up @@ -168,11 +184,11 @@ function default_attributes(::Type{Axis}, scene)
"The relative margins added to the autolimits in y direction."
yautolimitmargin = (0.05f0, 0.05f0)
"The xticks."
xticks = AbstractPlotting.automatic
xticks = LinearTicks(4)
"Format for xticks."
xtickformat = AbstractPlotting.automatic
"The yticks."
yticks = AbstractPlotting.automatic
yticks = LinearTicks(4)
"Format for yticks."
ytickformat = AbstractPlotting.automatic
"The button for panning."
Expand Down Expand Up @@ -206,6 +222,30 @@ function default_attributes(::Type{Axis}, scene)
yreversed = false
"Controls if the x axis goes rightwards (false) or leftwards (true)"
xreversed = false
"Controls if minor ticks on the x axis are visible"
xminorticksvisible = false
"The alignment of x minor ticks on the axis spine"
xminortickalign = 0f0
"The tick size of x minor ticks"
xminorticksize = 5f0
"The tick width of x minor ticks"
xminortickwidth = 1f0
"The tick color of x minor ticks"
xminortickcolor = :black
"The tick locator for the x minor ticks"
xminorticks = IntervalsBetween(2)
"Controls if minor ticks on the y axis are visible"
yminorticksvisible = false
"The alignment of y minor ticks on the axis spine"
yminortickalign = 0f0
"The tick size of y minor ticks"
yminorticksize = 5f0
"The tick width of y minor ticks"
yminortickwidth = 1f0
"The tick color of y minor ticks"
yminortickcolor = :black
"The tick locator for the y minor ticks"
yminorticks = IntervalsBetween(2)
end

(attributes = attrs, documentation = docdict, defaults = defaultdict)
Expand Down Expand Up @@ -248,7 +288,7 @@ function default_attributes(::Type{Colorbar}, scene)
"Controls if the tick marks are visible."
ticksvisible = true
"The ticks."
ticks = AbstractPlotting.automatic
ticks = LinearTicks(4)
"Format for ticks."
tickformat = AbstractPlotting.automatic
"The space reserved for the tick labels."
Expand Down Expand Up @@ -311,6 +351,18 @@ function default_attributes(::Type{Colorbar}, scene)
highclip = nothing
"The color of the low clip triangle."
lowclip = nothing
"Controls if minor ticks are visible"
minorticksvisible = false
"The alignment of minor ticks on the axis spine"
minortickalign = 0f0
"The tick size of minor ticks"
minorticksize = 5f0
"The tick width of minor ticks"
minortickwidth = 1f0
"The tick color of minor ticks"
minortickcolor = :black
"The tick locator for the minor ticks"
minorticks = IntervalsBetween(5)
end
(attributes = attrs, documentation = docdict, defaults = defaultdict)
end
Expand Down Expand Up @@ -500,6 +552,12 @@ function default_attributes(::Type{LineAxis})
ticklabelpad = 5f0,
labelpadding = 15f0,
reversed = false,
minorticksvisible = true,
minortickalign = 0f0,
minorticksize = 5f0,
minortickwidth = 1f0,
minortickcolor = :black,
minorticks = AbstractPlotting.automatic,
)
end

Expand Down
77 changes: 68 additions & 9 deletions src/makielayout/layoutables/axis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
backgroundcolor,
xlabelfont, ylabelfont, xticklabelfont, yticklabelfont,
flip_ylabel, xreversed, yreversed,
xminorticksvisible, xminortickalign, xminorticksize, xminortickwidth, xminortickcolor, xminorticks,
yminorticksvisible, yminortickalign, yminorticksize, yminortickwidth, yminortickcolor, yminorticks,
xminorgridvisible, yminorgridvisible, xminorgridwidth, yminorgridwidth,
xminorgridcolor, yminorgridcolor, xminorgridstyle, yminorgridstyle
)

decorations = Dict{Symbol, Any}()
Expand Down Expand Up @@ -68,6 +72,15 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
translate!(xgridlines, 0, 0, -10)
decorations[:xgridlines] = xgridlines

xminorgridnode = Node(Point2f0[])
xminorgridlines = linesegments!(
topscene, xminorgridnode, linewidth = xminorgridwidth, show_axis = false, visible = xminorgridvisible,
color = xminorgridcolor, linestyle = xminorgridstyle,
)
# put gridlines behind the zero plane so they don't overlay plots
translate!(xminorgridlines, 0, 0, -10)
decorations[:xminorgridlines] = xminorgridlines

ygridnode = Node(Point2f0[])
ygridlines = linesegments!(
topscene, ygridnode, linewidth = ygridwidth, show_axis = false, visible = ygridvisible,
Expand All @@ -77,6 +90,15 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
translate!(ygridlines, 0, 0, -10)
decorations[:ygridlines] = ygridlines

yminorgridnode = Node(Point2f0[])
yminorgridlines = linesegments!(
topscene, yminorgridnode, linewidth = yminorgridwidth, show_axis = false, visible = yminorgridvisible,
color = yminorgridcolor, linestyle = yminorgridstyle,
)
# put gridlines behind the zero plane so they don't overlay plots
translate!(yminorgridlines, 0, 0, -10)
decorations[:yminorgridlines] = yminorgridlines

onany(limits, xreversed, yreversed) do lims, xrev, yrev

nearclip = -10_000f0
Expand Down Expand Up @@ -154,7 +176,9 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
ticklabelspace = xticklabelspace, ticks = xticks, tickformat = xtickformat, ticklabelsvisible = xticklabelsvisible,
ticksvisible = xticksvisible, spinevisible = xspinevisible, spinecolor = xspinecolor, spinewidth = spinewidth,
ticklabelsize = xticklabelsize, trimspine = xtrimspine, ticksize = xticksize,
reversed = xreversed, tickwidth = xtickwidth, tickcolor = xtickcolor)
reversed = xreversed, tickwidth = xtickwidth, tickcolor = xtickcolor,
minorticksvisible = xminorticksvisible, minortickalign = xminortickalign, minorticksize = xminorticksize, minortickwidth = xminortickwidth, minortickcolor = xminortickcolor, minorticks = xminorticks,
)
decorations[:xaxis] = xaxis

yaxis = LineAxis(topscene, endpoints = yaxis_endpoints, limits = lift(ylimits, limits),
Expand All @@ -165,7 +189,9 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
ticklabelspace = yticklabelspace, ticks = yticks, tickformat = ytickformat, ticklabelsvisible = yticklabelsvisible,
ticksvisible = yticksvisible, spinevisible = yspinevisible, spinecolor = yspinecolor, spinewidth = spinewidth,
trimspine = ytrimspine, ticklabelsize = yticklabelsize, ticksize = yticksize, flip_vertical_label = flip_ylabel, reversed = yreversed, tickwidth = ytickwidth,
tickcolor = ytickcolor)
tickcolor = ytickcolor,
minorticksvisible = yminorticksvisible, minortickalign = yminortickalign, minorticksize = yminorticksize, minortickwidth = yminortickwidth, minortickcolor = yminortickcolor, minorticks = yminorticks,
)

decorations[:yaxis] = yaxis

Expand Down Expand Up @@ -221,6 +247,20 @@ function layoutable(::Type{<:Axis}, fig_or_scene::Union{Figure, Scene}; bbox = n
ygridnode[] = interleave_vectors(tickpos, opposite_tickpos)
end

on(xaxis.minortickpositions) do tickpos
pxheight = height(scene.px_area[])
offset = xaxisposition[] == :bottom ? pxheight : -pxheight
opposite_tickpos = tickpos .+ Ref(Point2f0(0, offset))
xminorgridnode[] = interleave_vectors(tickpos, opposite_tickpos)
end

on(yaxis.minortickpositions) do tickpos
pxwidth = width(scene.px_area[])
offset = yaxisposition[] == :left ? pxwidth : -pxwidth
opposite_tickpos = tickpos .+ Ref(Point2f0(offset, 0))
yminorgridnode[] = interleave_vectors(tickpos, opposite_tickpos)
end

titlepos = lift(scene.px_area, titlegap, titlealign, xaxisposition, xaxis.protrusion) do a,
titlegap, align, xaxisposition, xaxisprotrusion

Expand Down Expand Up @@ -707,11 +747,13 @@ function add_reset_limits!(la::Axis)
end

"""
hidexdecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true)
hidexdecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true,
minorgrid = true, minorticks = true)
Hide decorations of the x-axis: label, ticklabels, ticks and grid.
"""
function hidexdecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true)
function hidexdecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true,
minorgrid = true, minorticks = true)
if label
la.xlabelvisible = false
end
Expand All @@ -724,14 +766,22 @@ function hidexdecorations!(la::Axis; label = true, ticklabels = true, ticks = tr
if grid
la.xgridvisible = false
end
if minorgrid
la.xminorgridvisible = false
end
if minorticks
la.xminorticksvisible = false
end
end

"""
hideydecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true)
hideydecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true,
minorgrid = true, minorticks = true)
Hide decorations of the y-axis: label, ticklabels, ticks and grid.
"""
function hideydecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true)
function hideydecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true,
minorgrid = true, minorticks = true)
if label
la.ylabelvisible = false
end
Expand All @@ -744,16 +794,25 @@ function hideydecorations!(la::Axis; label = true, ticklabels = true, ticks = tr
if grid
la.ygridvisible = false
end
if minorgrid
la.yminorgridvisible = false
end
if minorticks
la.yminorticksvisible = false
end
end

"""
hidedecorations!(la::Axis)
Hide decorations of both x and y-axis: label, ticklabels, ticks and grid.
"""
function hidedecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true)
hidexdecorations!(la; label = label, ticklabels = ticklabels, ticks = ticks, grid = grid)
hideydecorations!(la; label = label, ticklabels = ticklabels, ticks = ticks, grid = grid)
function hidedecorations!(la::Axis; label = true, ticklabels = true, ticks = true, grid = true,
minorgrid = true, minorticks = true)
hidexdecorations!(la; label = label, ticklabels = ticklabels, ticks = ticks, grid = grid,
minorgrid = minorgrid, minorticks = minorticks)
hideydecorations!(la; label = label, ticklabels = ticklabels, ticks = ticks, grid = grid,
minorgrid = minorgrid, minorticks = minorticks)
end

"""
Expand Down
9 changes: 7 additions & 2 deletions src/makielayout/layoutables/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)
rightspinevisible, leftspinevisible, bottomspinevisible, topspinecolor,
leftspinecolor, rightspinecolor, bottomspinecolor, colormap, limits,
halign, valign, vertical, flipaxisposition, ticklabelalign, flip_vertical_label,
nsteps, highclip, lowclip)
nsteps, highclip, lowclip,
minorticksvisible, minortickalign, minorticksize, minortickwidth, minortickcolor, minorticks)

decorations = Dict{Symbol, Any}()

Expand Down Expand Up @@ -252,7 +253,11 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)
ticksvisible = ticksvisible, ticklabelpad = ticklabelpad, tickalign = tickalign,
tickwidth = tickwidth, tickcolor = tickcolor, spinewidth = spinewidth,
ticklabelspace = ticklabelspace, ticklabelcolor = ticklabelcolor,
spinecolor = :transparent, spinevisible = :false, flip_vertical_label = flip_vertical_label)
spinecolor = :transparent, spinevisible = :false, flip_vertical_label = flip_vertical_label,
minorticksvisible = minorticksvisible, minortickalign = minortickalign,
minorticksize = minorticksize, minortickwidth = minortickwidth,
minortickcolor = minortickcolor, minorticks = minorticks)

decorations[:axis] = axis

onany(axis.protrusion, vertical, flipaxisposition) do axprotrusion,
Expand Down
Loading

0 comments on commit aa9b4e7

Please sign in to comment.