# Makie Draw on Colorbar

**URL:** https://discourse.julialang.org/t/makie-draw-on-colorbar/128982
**Category:** Visualization
**Tags:** plotting, makie
**Created:** [May 13, 2025, 11:13pm UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982 "2025-05-13T23:13:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![andrew-saydjari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrew-saydjari/32/19896_2.png) [@andrew-saydjari](https://discourse.julialang.org/u/andrew-saydjari)
#### Post date: [May 13, 2025, 11:13pm UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/1 "2025-05-13T23:13:00Z")

</div>

I have been struggling to find the syntax for drawing a line on a color bar using CairoMakie. I just want to be able to get the colorbar axis and do something like hlines!(cbar.axis. 5, color=“red”).

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 13, 2025, 11:34pm UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/2 "2025-05-13T23:34:03Z")

</div>

There’s no extra colorbar axis scene like Axis has, the color cells are drawn directly into `colorbar.blockscene` at the right pixel coordinates. You could draw into that as well but there’s no convenience to give you the right coordinates

---

<div class="post-metadata">

### Author: ![andrew-saydjari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrew-saydjari/32/19896_2.png) [@andrew-saydjari](https://discourse.julialang.org/u/andrew-saydjari)
#### Post date: [May 14, 2025, 2:39am UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/3 "2025-05-14T02:39:58Z")

</div>

Do we think this is worth making a feature request? I would have no idea how to go about the solution you suggested if that space doesn’t know how to do the coordinate mapping. It is a major disadvantage relative to some of the packages people are migrating from (like matplotlib via PythonPlot.jl).

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [May 14, 2025, 3:26pm UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/4 "2025-05-14T15:26:22Z")

</div>

I was trying to achieve something similar the other day. To hack around it I attempted to place a separate axis with hidden spines and decorations on top of the color bar so that I could plot into it, but I could not figure out how to correctly place this axis so that it lined up perfectly with the colorbar. `@jules` – is there a not-too-complicated way of doing this?

Edit: Actually, I think I figured it out. The following code

```julia
using GLMakie

X = rand(20,20)
fig = Figure()
ax = Axis(fig[1,1])
hm = heatmap!(ax, X)
cbar = Colorbar(fig[1,2], hm)
clims = cbar.limits[]

clay = Makie.get_layout!(fig[1,2])
cbbox = clay.layoutobservables.computedbbox
top_axis = Axis(fig, bbox = cbbox)
xlims!(top_axis, 0, 1)
ylims!(top_axis, clims...)
hidedecorations!(top_axis)
hidespines!(top_axis)
hlines!(top_axis, 0.5; color = :red, linewidth = 5)

fig

```

produces this output:

 ![plot_on_colorbar](https://global.discourse-cdn.com/julialang/original/3X/d/e/de8e0ccd1f86c422ef6f35cc2788286ab207c823.png)

It does seem to rely on some internals though, so probably not future proof.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [May 15, 2025, 12:08am UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/5 "2025-05-15T00:08:16Z")

</div>

What I do is just put an axis into the same figure layout position as the colorbar, and assign the same limits as the colorrange has:

```julia
	Colorbar(fig[1,2]; colorrange, ...)
	Axis(fig[1,2], limits=(0..1, colorrange))
	hidedecorations!()
	scatter!(...) # draws on this new axis

```

No internals, and approved by @jules on slack recently 🙂

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 15, 2025, 12:25am UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/6 "2025-05-15T00:25:03Z")

</div>

This will break if you enable high or lowclip though, as the axis will not adjust for the triangles. But the inner box of the colorbar is not exposed to the outside

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 15, 2025, 10:15pm UTC](https://discourse.julialang.org/t/makie-draw-on-colorbar/128982/7 "2025-05-15T22:15:44Z")

</div>

Here’s one way that works also with highclip/lowclip, although it uses an internal detail, the input arguments of the second plot of the colorbar, which is an image that is given the rectangle that the helper axis should also be placed at.

```julia
using CairoMakie

f = Figure()
ax = Axis(f[1, 1])
hm = heatmap!(ax, rand(10, 10) .* 100 .+ 10, colorrange = (20, 100), highclip = :cyan, lowclip = :black,
    colormap = :plasma, colorscale = log10)
cb = Colorbar(f[1, 2], hm, ticks = 20:10:100)

rect = lift(cb.blockscene.plots[2][1], cb.blockscene.plots[2][2]) do (xmin, xmax), (ymin, ymax)
    BBox(xmin, xmax, ymin, ymax)
end
inside_ax = Axis(f, bbox = rect, limits = @lift((0, 1, $(cb.limits)...)), yscale = cb.scale)
hidedecorations!(inside_ax)
hidespines!(inside_ax)

hlines!(inside_ax, 20:10:100, color = :lime, linewidth = 3)

f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/5/1/51a7ab15f72bf3809fbef202f7aa466c2c16e1d5.png)
