# 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:** 1\
**Showing post:** 14

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

---

_[View the full topic](https://discourse.julialang.org/t/add-a-colorbar-to-a-viz-in-geostats/112193)._
