# Axis

## Creating an Axis

The `Axis`

is a 2D axis that works well with automatic layouts. Here's how you create one

```
using CairoMakie
f = Figure()
ax = Axis(f[1, 1], xlabel = "x label", ylabel = "y label",
title = "Title")
f
```

## Plotting into an Axis

You can use all the normal mutating 2D plotting functions with an `Axis`

. These functions return the created plot object. Omitting the `ax`

argument plots into the `current_axis()`

, which is usually the axis that was last created.

```
lineobject = lines!(ax, 0..10, sin, color = :red)
scatobject = scatter!(0:0.5:10, cos, color = :orange)
f
```

## Deleting plots

You can delete a plot object directly via `delete!(ax, plotobj)`

. You can also remove all plots with `empty!(ax)`

.

```
using CairoMakie
f = Figure()
axs = [Axis(f[1, i]) for i in 1:3]
scatters = map(axs) do ax
[scatter!(ax, 0:0.1:10, x -> sin(x) + i) for i in 1:3]
end
delete!(axs[2], scatters[2][2])
empty!(axs[3])
f
```

## Setting Axis limits and reversing axes

You can set axis limits with the functions `xlims!`

, `ylims!`

or `limits!`

. The numbers are meant in the order left right for `xlims!`

, and bottom top for `ylims!`

. Therefore, if the second number is smaller than the first, the respective axis will reverse. You can manually reverse an axis by setting `ax.xreversed = true`

or `ax.yreversed = true`

.

Note that if you enforce an aspect ratio between x-axis and y-axis using `autolimitaspect`

, the values you set with these functions will probably not be exactly what you get, but they will be changed to fit the chosen ratio.

```
using CairoMakie
f = Figure()
axes = [Axis(f[i, j]) for j in 1:3, i in 1:2]
for (i, ax) in enumerate(axes)
ax.title = "Axis $i"
poly!(ax, Point2f0[(9, 9), (3, 1), (1, 3)],
color = cgrad(:inferno, 6, categorical = true)[i])
end
xlims!(axes[1], [0, 10]) # as vector
xlims!(axes[2], 10, 0) # separate, reversed
ylims!(axes[3], 0, 10) # separate
ylims!(axes[4], (10, 0)) # as tuple, reversed
limits!(axes[5], 0, 10, 0, 10) # x1, x2, y1, y2
limits!(axes[6], BBox(0, 10, 0, 10)) # as rectangle
f
```

### Setting half-automatic limits

You can set half limits by either giving one argument as `nothing`

or by using the keyword syntax where only `low`

or `high`

is given.

```
using CairoMakie
f = Figure()
data = rand(100, 2) .* 0.7 .+ 0.15
Axis(f[1, 1], title = "xlims!(nothing, 1)")
scatter!(data)
xlims!(nothing, 1)
Axis(f[1, 2], title = "xlims!(low = 0)")
scatter!(data)
xlims!(low = 0)
Axis(f[2, 1], title = "ylims!(0, nothing)")
scatter!(data)
ylims!(0, nothing)
Axis(f[2, 2], title = "ylims!(high = 1)")
scatter!(data)
ylims!(high = 1)
f
```

This also works when specifying limits directly, such as `Axis(..., limits = (nothing, 1, 2, nothing))`

.

### Auto-reset behavior

When you create a new plot in an axis, `reset_limits!(ax)`

is called, which adjusts the limits to the new bounds. If you have previously set limits with `limits!`

, `xlims!`

or `ylims!`

, these limits are not overridden by the new plot. If you want to override the manually set limits, call `autolimits!(ax)`

to compute completely new limits from the axis content.

The user-defined limits are stored in `ax.limits`

. This can either be a tuple with two entries, where each entry can be either `nothing`

or a tuple with numbers `(low, high)`

.It can also be a tuple with four numbers `(xlow, xhigh, ylow, yhigh)`

. You can pass this directly when creating a new axis. The same observable `limits`

is also set using `limits!`

, `xlims!`

and `ylims!`

, or reset to `(nothing, nothing)`

using `autolimits!`

.

```
using CairoMakie
f = Figure()
lines(f[1, 1], 0..10, sin)
lines(f[1, 2], 0..10, sin, axis = (limits = (0, 10, -1, 1),))
f
```

## Modifying ticks

To control ticks, you can set the axis attributes `xticks/yticks`

and `xtickformat/ytickformat`

.

You can overload one or more of these three functions to implement custom ticks:

```
tickvalues, ticklabels = MakieLayout.get_ticks(ticks, scale, formatter, vmin, vmax)
tickvalues = MakieLayout.get_tickvalues(ticks, vmin, vmax)
ticklabels = MakieLayout.get_ticklabels(formatter, tickvalues)
```

If you overload `get_ticks`

, you have to compute both tickvalues and ticklabels directly as a vector of floats and strings, respectively. Otherwise the result of `get_tickvalues`

is passed to `get_ticklabels`

by default. The limits of the respective axis are passed as `vmin`

and `vmax`

.

A couple of behaviors are implemented by default. You can specify static ticks by passing an iterable of numbers. You can also pass a tuple with tick values and tick labels directly, bypassing the formatting step.

As a third option you can pass a function taking minimum and maximum axis value as arguments and returning either a vector of tickvalues which are then passed to the current formatter, or a tuple with tickvalues and ticklabels which are then used directly.

For formatting, you can pass a function which takes a vector of numbers and outputs a vector of strings. You can also pass a format string which is passed to `Formatting.format`

from Formatting.jl, where you can mix the formatted numbers with other text like in `"{:.2f}ms"`

.

### Predefined ticks

The default tick type is `LinearTicks(n)`

, where `n`

is the target number of ticks which the algorithm tries to return.

```
using CairoMakie
fig = Figure()
for (i, n) in enumerate([2, 5, 9])
lines(fig[i, 1], 0..20, sin, axis = (xticks = LinearTicks(n),))
end
fig
```

There's also `WilkinsonTicks`

which uses the alternative Wilkinson algorithm.

`MultiplesTicks`

can be used when an axis should be marked at multiples of a certain number. A common scenario is plotting a trigonometric function which should be marked at pi intervals.

```
using CairoMakie
lines(0..20, sin, axis = (xticks = MultiplesTicks(4, pi, "π"),))
```

Here are a couple of examples that show off different settings for ticks and formats.

```
using CairoMakie
scene, layout = layoutscene(resolution = (1200, 900))
axes = layout[] = [Axis(scene) for i in 1:2, j in 1:2]
xs = LinRange(0, 2pi, 50)
for (i, ax) in enumerate(axes)
ax.title = "Axis $i"
lines!(ax, xs, sin.(xs))
end
axes[1].xticks = 0:6
axes[2].xticks = 0:pi:2pi
axes[2].xtickformat = xs -> ["$(x/pi)π" for x in xs]
axes[3].xticks = (0:pi:2pi, ["start", "middle", "end"])
axes[4].xticks = 0:pi:2pi
axes[4].xtickformat = "{:.2f}ms"
axes[4].xlabel = "Time"
scene
```

## 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.

```
using CairoMakie
theme = Attributes(
Axis = (
xminorticksvisible = true,
yminorticksvisible = true,
xminorgridvisible = true,
yminorgridvisible = true,
)
)
fig = with_theme(theme) do
fig = Figure()
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
fig
```

## Hiding Axis spines and decorations

You can hide all axis elements manually, by setting their specific visibility attributes to `false`

, like `xticklabelsvisible`

, but that can be tedious. There are a couple of convenience functions for this.

To hide spines, you can use `hidespines!`

.

```
using CairoMakie
f = Figure()
ax1 = Axis(f[1, 1], title = "Axis 1")
ax2 = Axis(f[1, 2], title = "Axis 2")
hidespines!(ax1)
hidespines!(ax2, :t, :r) # only top and right
f
```

To hide decorations, you can use `hidedecorations!`

, or the specific `hidexdecorations!`

and `hideydecorations!`

. 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.

```
using CairoMakie
f = Figure()
ax1 = Axis(f[1, 1], title = "Axis 1")
ax2 = Axis(f[1, 2], title = "Axis 2")
ax3 = Axis(f[1, 3], title = "Axis 3")
hidedecorations!(ax1)
hidexdecorations!(ax2, grid = false)
hideydecorations!(ax3, ticks = false)
f
```

## Log scales and other axis scales

The two attributes `xscale`

and `yscale`

, which by default are set to `identity`

, can be used to project the data in a nonlinear way, in addition to the linear zoom that the limits provide.

Take care that the axis limits always stay inside the limits appropriate for the chosen scaling function, for example, `log`

functions fail for values `x <= 0`

, `sqrt`

for `x < 0`

, etc.

```
using CairoMakie
data = LinRange(0.01, 0.99, 200)
f = Figure(resolution = (800, 800))
for (i, scale) in enumerate([identity, log10, log2, log, sqrt, Makie.logit])
row, col = fldmod1(i, 2)
Axis(f[row, col], yscale = scale, title = string(scale),
yminorticksvisible = true, yminorgridvisible = true,
yminorticks = IntervalsBetween(8))
lines!(data, color = :blue)
end
f
```