# Add a colorbar to a viz in GeoStats

**URL:** https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193
**Category:** New to Julia
**Tags:** geostats
**Created:** [March 27, 2024, 5:00pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193 "2024-03-27T17:00:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [March 27, 2024, 5:00pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/1 "2024-03-27T17:00:37Z")

</div>

How can I add a colour bar to this map in GeoStat?

```julia
viz(longt.geometry, color = log10.(longt.rat), colorscheme = colorschemes[:hawaii], facetcolor = "black", showfacets=true)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/c/2c3af3d1088b4a5fcb81f8932acd87596f0d6ab5.png)

Thanks!

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 27, 2024, 5:17pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/2 "2024-03-27T17:17:41Z")

</div>

You can do it as you would normally do with Makie.jl. The `viz` recipe returns the figure and axis:

```julia
fig, ax = viz(...)

```

You can then pass these objects to the [Colorbar](https://docs.makie.org/stable/reference/blocks/colorbar) constructor. Alternatively, you can send the geotable to the viewer, which already comes with the colorbar:

```julia
geotable |> viewer

```

Note that the `viz` recipe is called automatically if you pass a `geotable.geometry` to the standard `Makie.plot` command. The name `viz` is just shorter to type.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 27, 2024, 5:22pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/3 "2024-03-27T17:22:11Z")

</div>

As a side note, you can pass `colorscheme = :hawaii` directly.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [March 27, 2024, 5:28pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/4 "2024-03-27T17:28:01Z")

</div>

> [@juliohm](#):
>
> `fig, ax = `

The Makie “Basic” page shows:

```julia
fig, ax, hm = heatmap(randn(20, 20))
Colorbar(fig[1, 2], hm)
fig

```

How do I tell Colorbar the hm part?

```julia
fig, ax, v = viz(longt.geometry, color = log10.(longt.rat), colorscheme = :hawaii, facetcolor = "black", showfacets=true)
Mke.Colorbar(fig[1, 2], v)

```

tells me `Multiple colormaps found for plot` at that second line.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 27, 2024, 5:32pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/5 "2024-03-27T17:32:52Z")

</div>

Can you share a MWE with the same type of domain you are trying? Some of the Makie.jl functions don’t work well with `Colorbar` as far as I remember, and we had to build the colorbar differently in the `viewer`.

You can check the source code of the `viewer` here:

> <https://github.com/JuliaEarth/GeoTables.jl/blob/f015182c11d2a8bc67f0ad337725ba5f30568e24/ext/GeoTablesMakieExt/viewer.jl#L5>

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [March 27, 2024, 5:36pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/6 "2024-03-27T17:36:28Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [March 27, 2024, 11:52pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/7 "2024-03-27T23:52:22Z")

</div>

> [@juliohm](#):
>
> Can you share a MWE with the same type of domain you are trying?

```julia
using GeoStats, GeoIO, CSV, DataFrames, Colors, ColorSchemes
import CairoMakie as Mke

# List of London Boroughs
lon = ["Westminster", "Ealing", "Kingston upon Thames", "Islington", "Merton", "Hackney", "Camden", "Wandsworth", "Enfield", "Southwark", "City of London", "Lambeth", "Hillingdon", "Tower Hamlets", "Kensington and Chelsea", "Greenwich", "Haringey", "Richmond upon Thames", "Hounslow", "Harrow", "Lewisham", "Hammersmith and Fulham", "Newham", "Waltham Forest", "Croydon", "Brent", "Redbridge", "Barnet", "Bromley", "Barking and Dagenham", "Havering", "Sutton", "Bexley"]

# Shape file from ONS: https://geoportal.statistics.gov.uk/datasets/127c4bda06314409a1fa0df505f510e6_0/explore
LAs = GeoIO.load("LAD_DEC_2023_UK_BFC.shp") |> Rename("LAD23CD" => "LADcode")

#combo = CSV.read("combo.csv", DataFrame)

# Subset of CSV data just covering London
combo = DataFrame(
    LADcode=["E09000033", "E09000009", "E09000021", "E09000019", "E09000024", "E09000012", "E09000007", "E09000032", "E09000010", "E09000028", "E09000001", "E09000022", "E09000017", "E09000030", "E09000020", "E09000011", "E09000014", "E09000027", "E09000018", "E09000015", "E09000023", "E09000013", "E09000025", "E09000031", "E09000008", "E09000005", "E09000026", "E09000003", "E09000006", "E09000002", "E09000016", "E09000029", "E09000004"],
    newLocalAuthority=["Westminster", "Ealing", "Kingston upon Thames", "Islington", "Merton", "Hackney", "Camden", "Wandsworth", "Enfield", "Southwark", "City of London", "Lambeth", "Hillingdon", "Tower Hamlets", "Kensington and Chelsea", "Greenwich", "Haringey", "Richmond upon Thames", "Hounslow", "Harrow", "Lewisham", "Hammersmith and Fulham", "Newham", "Waltham Forest", "Croydon", "Brent", "Redbridge", "Barnet", "Bromley", "Barking and Dagenham", "Havering", "Sutton", "Bexley"],
    Total_Awarded=[1551248258, 110166558, 38033540, 865003501, 46621989, 395602425, 915273792, 197296888, 86772623, 573171051, 321575633, 665770100, 39514648, 374049415, 307431197, 812345843, 181261693, 176262413, 131028168, 48236040, 173721339, 257659057, 246970981, 95473179, 86525712, 304128061, 77681457, 82934564, 88862200, 63604751, 46974062, 42463929, 41183878],
    subpop=[205087, 366127, 167845, 216767, 215324, 259956, 210390, 328367, 329601, 306374, 8618, 317498, 304792, 312273, 143940, 289254, 264130, 195232, 287940, 260987, 299810, 183295, 350626, 278050, 390506, 338918, 309836, 388639, 329830, 218534, 262022, 209517, 246543]
    )

# Funding per capita
natave = sum(skipmissing(combo.Total_Awarded)) / sum(skipmissing(combo.subpop))
combo.rat .= (combo.Total_Awarded) ./ (combo.subpop) ./ natave

# Add combo to the GeoTable
table1 = values(LAs) |> DataFrame
table1.index = [i for i in 1:nrow(table1)]
table = leftjoin(table1, combo, on=:LADcode, matchmissing=:equal, order=:left)
sort!(table, :index)
gt = georef(table, LAs.geometry)

# Draw the map with colorbar
longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in lon)
fig, ax, v = viz(longt.geometry, color=log10.(longt.rat), colorscheme=:hawaii, facetcolor="black", showfacets=true)
Mke.Colorbar(fig[1, 2], v)
Mke.current_figure()

```

Produces this stacktrace in VScode:

```julia
ERROR: LoadError: Multiple colormaps found for plot MakieCore.Poly{Tuple{Vector{GeometryBasics.Polygon{2, Float64, GeometryBasics.Point2{Float64}, GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}, Vector{GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}}}}}}, please specify which one to use manually. Please overload `Makie.extract_colormap(::MakieCore.Poly{Tuple{Vector{GeometryBasics.Polygon{2, Float64, GeometryBasics.Point2{Float64}, GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}, Vector{GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}}}}}})` to allow for the automatical creation of a Colorbar.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] extract_colormap_recursive(plot::MakieCore.Poly{Tuple{Vector{GeometryBasics.Polygon{2, Float64, GeometryBasics.Point2{Float64}, GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}, Vector{GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}}}}}})
   @ Makie C:\Users\TGebbels\.julia\packages\Makie\VRavR\src\makielayout\blocks\colorbar.jl:93
 [3] (::Makie.var"#1836#1838")(child::MakieCore.Poly{Tuple{Vector{GeometryBasics.Polygon{2, Float64, GeometryBasics.Point2{Float64}, GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}, Vector{GeometryBasics.LineString{2, Float64, GeometryBasics.Point2{Float64}, Base.ReinterpretArray{GeometryBasics.Line{2, Float64}, 1, Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, GeometryBasics.TupleView{Tuple{GeometryBasics.Point2{Float64}, GeometryBasics.Point2{Float64}}, 2, 1, Vector{GeometryBasics.Point2{Float64}}}, false}}}}}}})
   @ Makie .\none:0
 [4] iterate
   @ .\generator.jl:47 [inlined]
 [5] collect(itr::Base.Generator{Vector{MakieCore.Plot}, Makie.var"#1836#1838"})
   @ Base .\array.jl:834
 [6] extract_colormap_recursive(plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}})
   @ Makie C:\Users\TGebbels\.julia\packages\Makie\VRavR\src\makielayout\blocks\colorbar.jl:84
 [7] Makie.Colorbar(fig_or_scene::GridLayoutBase.GridPosition, plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}}; kwargs::@Kwargs{})
   @ Makie C:\Users\TGebbels\.julia\packages\Makie\VRavR\src\makielayout\blocks\colorbar.jl:100
 [8] Makie.Colorbar(fig_or_scene::GridLayoutBase.GridPosition, plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}})
   @ Makie C:\Users\TGebbels\.julia\packages\Makie\VRavR\src\makielayout\blocks\colorbar.jl:98
 [9] top-level scope
   @ c:\Users\TGebbels\...\Documents\DCMS Database\GeoStatMWE.jl:37
in expression starting at c:\Users\TGebbels\...\Documents\DCMS Database\GeoStatMWE.jl:37

```

Executing in `Pluto` produces a nice map at the first `viz` but the same error when trying to add the colorbar.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 28, 2024, 9:36am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/8 "2024-03-28T09:36:51Z")

</div>

Thank you for sharing the MWE @TimG . From the Makie.jl Colorbar docs, only a reduced set of built-in recipes support automatic creation of colorbar from the plot object:

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

When you write `fig, ax, plt = viz(...)` the third argument is this plot object.

At present, the only solution we can provide on our side is create the colorbar manually. You can do it based on the vector of colors you want to plot:

```julia
julia> color = log10.(longt.rat)
33-element Vector{Float64}:
  1.5409978534680409
 -0.5669082482422928
 -0.7016896041108623
 -0.8080442275518339
 -0.07791693181813554
 -0.6004518836862073
  0.607647143339695
 -0.6853613193412048
  ⋮
 -0.07527008204388734
  0.24115355077977496
 -0.724077881880776
  0.04751575435515542
 -0.495120305739368
 -0.2521180600836897
  0.8478643845407627

julia> cbar = Mke.Colorbar(fig[1,2], limits = extrema(color), colormap = "hawaii")
Makie.Colorbar()

julia> fig

```

![image](https://global.discourse-cdn.com/julialang/original/3X/c/0/c08ca3c2ad74866286133029c1f3b05db325c48d.png)

We will see what we can do to improve this situation, probably submitting a PR to Makie.jl at some point to fix this elsewhere.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 28, 2024, 9:44am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/9 "2024-03-28T09:44:14Z")

</div>

Opened an issue: [Review colorbar creation with `viz` and `viewer` · Issue #413 · JuliaEarth/GeoStats.jl · GitHub](https://github.com/JuliaEarth/GeoStats.jl/issues/413)

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [March 28, 2024, 10:00am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/10 "2024-03-28T10:00:24Z")

</div>

> [@juliohm](#):
>
> `cbar = Mke.Colorbar(fig[1,2], limits = extrema(color), colormap = "hawaii")`

This works well. Thank you.

Now, at the risk of overloading this topic, how can I make the colors consistent across multiple maps of different cities. For example, here are Manchester and London compared:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/de4f7c27ad278febf4f13ad7dd53ffc2fb86f006.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e0e27ca1bd24141cd29efb70da30225f77ea9ee.png)  
The colormap is autoscaled to the data in each map, so the colors are not comparable between the two. Can I fix this, so that the colorbar is consistent across multiple maps?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 28, 2024, 10:25am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/11 "2024-03-28T10:25:48Z")

</div>

That is something that we need to brainstorm. Currently, our `viz` takes the colorscheme and the provided values to produce the final colors. This is the purpose of the function `ascolors`:

> <https://github.com/JuliaGeometry/Meshes.jl/blob/a792987c67059c0fe4a18a498611b97403eb533d/ext/colors.jl#L8-L12>

Given any Julia object, you can add a method to this function to determine a different behavior. We already defined the behavior for vector of `Number` as you can see, which simply gets the color from the colorscheme object with the `:extrema` flag.

I understand that you need to create two separate plots with the same range of colors, without scaling the values to fit the entire range of the colorscheme. Perhaps this calls for new options `cmin` and `cmax` to control de extrema of the colorscheme.

You can try to overwrite the method above with a different behavior to see it if works. If it does, please feel free to submit a pull request with the proposed change.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [March 28, 2024, 12:55pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/12 "2024-03-28T12:55:27Z")

</div>

If `viz` supported the `Makie` option `colorrange=`, then I could specify a common `colorrange` based on the range of the data nationally. It is already easy to do this for the `colorbar` itself.

```julia
color = log10.(combo.rat) # combo (from the CSV) is a national dataset
lim=extrema(color)
                                         # subset London data
longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in lon) 
                                         # use colorrange to specify the national extrema 
fig, ax, v = viz(longt.geometry, color=log10.(longt.rat), colorscheme=:hawaii, colorrange=lim, facetcolor="black", showfacets=true)
                                         # define the colorbar based on the national extrema
cbar = Mke.Colorbar(fig[1, 2], limits=lim, colormap="hawaii")
Mke.display(fig)

```

but this doesn’t seem to work.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 28, 2024, 1:03pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/13 "2024-03-28T13:03:54Z")

</div>

That is a very good idea. We will investigate how to forward the `colorrange` option to adjust the selected colors from the values. This option can have a default `colorrange=:extrema` to reproduce the current behavior.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [April 8, 2024, 8:07am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/14 "2024-04-08T08:07:13Z")

</div>

So it turns out to be easy to persuade `viz` to use a consistent colour range between plots.

I am mapping a set of `n` areas based on an n-row GeoTable. I give the data that `viz` uses to map colour using the `color=` kwarg. This takes a vector of data that is used to scale to the specified `colorscheme`. The color vector can be longer than n values and, in this case, `viz` will just use the first n values to define the colours of the n areas. Extra values are ignored as far as the map is concerned but they are used in the scaling of the data to the `colorscheme`. I can simply add two extra values to the vector, so there are n+2 values, where the extra two values are the minimum and maximum data values I want to use for the colour range. In my example, I can use the extrema of the national dataset:

```julia
        natave = sum(skipmissing(combo.Total_Awarded)) / sum(skipmissing(combo.subpop))
        combo.rat .= combo.Total_Awarded ./ combo.subpop ./ natave
        combo.col .= log10.(combo.rat)
        cr = extrema(combo.col)

```

And then I can plot the maps like this:

```julia
function citymap(gt, df, cr)
    fig = Mke.Figure()
    ax = []
    for (i, met) in enumerate(eachrow(df))
        longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in met.boroughs)
        crange = longt.col
        append!(crange, cr)
        push!(ax, Mke.Axis(fig[div(i - 1, 2)+1, mod(i - 1, 2)+1], title=met.metname, titlesize=10.0f0, 
                    limits=(met.xaxismin, met.xaxismax, met.yaxismin, met.yaxismax),
                    xlabel="BNG Easting", xlabelsize=8.0f0, xtickformat="{:.0f}", xticklabelsize=6.0f0,
                    ylabel="BNG Northing", ylabelsize=8.0f0, ytickformat="{:.0f}", yticklabelsize=6.0f0))
        viz!(ax[i], longt.geometry, color=crange, colorscheme=:hawaii, facetcolor="black", showfacets=true, segmentsize=0.3f0)
        ax[i].aspect = Mke.DataAspect()
    end
    Mke.Colorbar(fig[1:2, 3], limits=extrema(gt.rat), colormap="hawaii", scale=log10, tickformat="{:.1f}",
        ticklabelsize=8.0f0, label="Multiple of national average funding per capita", labelsize=10.0f0)
    Mke.display(fig)
end

```

And this will produce four maps that all share the same colormap!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/396bd27188f37641282168b8a78d3f4190c0d9a6.png)

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [April 8, 2024, 8:45am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/15 "2024-04-08T08:45:49Z")

</div>

That is great @TimG. We will soon release a new minor version of the project with a more flexible method to set the color range. Thanks for sharing the update and workaround.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [April 8, 2024, 5:04pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/16 "2024-04-08T17:04:38Z")

</div>

@TimG GeoStats.jl v0.54 is out with the new `colorrange` option in `viz` and a new `cbar` command for colorbars. You should now be able to fix the color range in each subplot without the workaround above.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [April 9, 2024, 8:50am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/17 "2024-04-09T08:50:33Z")

</div>

Thank you for this update.  
I don’t seem able to make it work immediately. My error, surely, but I’m afraid I need a hint beyond what is in the docs. I took out my workaround and changed the viz! call using the new keywords:

```julia
function citymap(gt, df, cr)
    fig = Mke.Figure()
    ax = []
    for (i, met) in enumerate(eachrow(df))
        longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in met.boroughs)
# crange = longt.col
# append!(crange, cr)
        push!(ax, Mke.Axis(fig[div(i - 1, 2)+1, mod(i - 1, 2)+1], title=met.metname, titlesize=10.0f0, 
                    limits=(met.xaxismin, met.xaxismax, met.yaxismin, met.yaxismax),
                    xlabel="BNG Easting", xlabelsize=8.0f0, xtickformat="{:.0f}", xticklabelsize=6.0f0,
                    ylabel="BNG Northing", ylabelsize=8.0f0, ytickformat="{:.0f}", yticklabelsize=6.0f0))
        viz!(ax[i], longt.geometry, color=longt.col, colormap=:hawaii, colorrange=cr, segmentcolor="black", showsegments=true, segmentsize=0.3f0)
        ax[i].aspect = Mke.DataAspect()
    end
    Mke.Colorbar(fig[1:2, 3], limits=extrema(gt.rat), colormap="hawaii", scale=log10, tickformat="{:.1f}",
        ticklabelsize=8.0f0, label="Multiple of national average funding per capita", labelsize=10.0f0)
    Mke.display(fig)
end

```

but this generates

```julia
ERROR: LoadError: MethodError: no method matching getcolors(::Colorfy.Colorfier{Vector{Union{Missing, Float64}}, Vector{Float64}, ColorScheme{Vector{RGB{Float64}}, String, String}, Tuple{Float64, Float64}})

Closest candidates are:
  getcolors(::Colorfy.Colorfier{<:AbstractVector{<:Distribution}})
   @ ColorfyDistributionsExt C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\ext\ColorfyDistributionsExt.jl:11
  getcolors(::Colorfy.Colorfier{<:AbstractVector{<:Quantity}})
   @ ColorfyUnitfulExt C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\ext\ColorfyUnitfulExt.jl:11
  getcolors(::Colorfy.Colorfier{<:AbstractVector{<:CategoricalValue}})
   @ ColorfyCategoricalArraysExt C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\ext\ColorfyCategoricalArraysExt.jl:12
  ...

Stacktrace:
  [1] colors(colorfier::Colorfy.Colorfier{Vector{Union{Missing, Float64}}, Vector{Float64}, ColorScheme{Vector{RGB{Float64}}, String, String}, Tuple{Float64, Float64}})
    @ Colorfy C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\src\Colorfy.jl:146
  [2] colorfy(values::Vector{Union{Missing, Float64}}; kwargs::@Kwargs{alphas::Vector{Float64}, colorscheme::Symbol, colorrange::Tuple{Float64, Float64}})
    @ Colorfy C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\src\Colorfy.jl:50
  [3] colorfy
    @ C:\Users\TGebbels\.julia\packages\Colorfy\zvEia\src\Colorfy.jl:50 [inlined]
  [4] process
    @ C:\Users\TGebbels\.julia\packages\Meshes\NR5lE\ext\colors.jl:10 [inlined]
  [5] (::MeshesMakieExt.var"#208#216")(arg1#334::Symbol, arg2#335::Vector{Union{Missing, Float64}}, arg3#336::Tuple{Float64, Float64}, arg4#337::Nothing)
    @ MeshesMakieExt .\none:0
  [6] map(::MeshesMakieExt.var"#208#216", ::Observables.Observable{Any}, ::Observables.Observable{Any}, ::Vararg{Observables.Observable{Any}}; ignore_equal_values::Bool, priority::Int64)
    @ Observables C:\Users\TGebbels\.julia\packages\Observables\YdEbO\src\Observables.jl:570
  [7] map(::MeshesMakieExt.var"#208#216", ::Observables.Observable{Any}, ::Observables.Observable{Any}, ::Observables.Observable{Any}, ::Vararg{Observables.Observable{Any}})
    @ Observables C:\Users\TGebbels\.julia\packages\Observables\YdEbO\src\Observables.jl:568
  [8] plot!(plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}})
    @ MeshesMakieExt C:\Users\TGebbels\.julia\packages\Meshes\NR5lE\ext\geometryset.jl:13
  [9] connect_plot!(parent::Makie.Scene, plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}})
    @ Makie C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\interfaces.jl:260
 [10] plot!
    @ C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\interfaces.jl:265 [inlined]
 [11] plot!(ax::Makie.Axis, plot::MakieCore.Plot{Meshes.viz, Tuple{GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}}})
    @ Makie C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\figureplotting.jl:316
 [12] _create_plot!(::Function, ::Dict{Symbol, Any}, ::Makie.Axis, ::SubDomain{2, Float64, GeometrySet{2, Float64, MultiPolygon{2, Float64, PolyArea{2, Float64, Ring{2, Float64, CircularArrays.CircularVector{Point2, Vector{Point2}}}}}}, Vector{Int64}})
    @ Makie C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\figureplotting.jl:284
 [13] #viz!#2
    @ C:\Users\TGebbels\.julia\packages\MakieCore\UAwps\src\recipes.jl:176 [inlined]
 [14] citymap(gt::GeoTable{DataFrame}, df::DataFrame, cr::Tuple{Float64, Float64})
    @ Main c:\Users\TGebbels\...\Documents\DCMS Database\GeoStat City Maps.jl:57
 [15] macro expansion
    @ .\timing.jl:279 [inlined]
 [16] main()
    @ Main c:\Users\TGebbels\...\Documents\DCMS Database\GeoStat City Maps.jl:122
 [17] top-level scope
    @ c:\Users\TGebbels\...\Documents\DCMS Database\GeoStat City Maps.jl:125
in expression starting at c:\Users\TGebbels\...\Documents\DCMS Database\GeoStat City Maps.jl:125

```

Also, I’m assuming that I can simply replace the `Mke.Colorbar()` call with an identical `cbar()` call?

Thanks,

Tim

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [April 9, 2024, 10:34am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/18 "2024-04-09T10:34:08Z")

</div>

Thank you for reporting the issue @TimG, this is probably a dispatch issue in the new Colorfy.jl package. We will release a patch soon with a fix.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [April 9, 2024, 10:40am UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/19 "2024-04-09T10:40:36Z")

</div>

> [@TimG](#):
>
> Also, I’m assuming that I can simply replace the `Mke.Colorbar()` call with an identical `cbar()` call?

Exactly. You can pass the options to `cbar` and it will use the same mechanism to generate colors that is used in `viz/viz!`.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [April 9, 2024, 12:12pm UTC](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193/20 "2024-04-09T12:12:07Z")

</div>

Hi Tim, can you please share a MWE? We can’t reproduce the error.

[Next page](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193.md?page=2)
