# Plotting a thematic map with a continuous color scale

**URL:** <https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695>\
**Category:** General Usage\
**Tags:** plotting, plots, shapefile\
**Created:** [May 26, 2022, 6:28am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695 "2022-05-26T06:28:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 26, 2022, 6:28am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/1 "2022-05-26T06:28:22Z")

</div>

I would like to plot a thematic map (choropleth) using `Plots.jl`. The DataFrame `df` I use has 3 columns: `geometry`, `value1` and `value2`. The column `geometry` is of element type `Polygon` (data was read using the `Shapefile` package). The columns `value1` and `value2` are of element type `Int64` with values \>= 0.

`plot(df.geometry, color = :lightblue)` creates a map with `lightblue` polygons without issues (and without colorbar to the side).

However, I would like the `value2` column to color the polygons (shapes) based on a colormap/colorscheme like `viridis` with a corresponding colorbar to the side as legend.

> Any suggestions how to do this?

`plot(shp.geometry, palette = ColorSchemes.viridis)` does plot a coloured map, however it is not clear on which variable this colours are based. I would like to specify that manually (`value2` column) and have the colorbar added.

[![enter image description here](https://global.discourse-cdn.com/julialang/original/3X/6/5/658f923eaf33e58b3502986610558d9d9304dc58.png)](https://i.stack.imgur.com/FzwKY.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 10:21am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/2 "2022-05-26T10:21:09Z")

</div>

What happens if you add the plot argument: `colorbar=true`? Also, try argument: `line_z = df.value2`

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 26, 2022, 10:28am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/3 "2022-05-26T10:28:45Z")

</div>

Thanks for your reply. However, adding `colorbar = true` does not show a colorbar. And adding `line_z = df.value2` does throw an error.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 10:31am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/4 "2022-05-26T10:31:12Z")

</div>

> [@Brinkhuis](#):
>
> `line_z = df.value2` does throw an error

And `fill_z = df.value2` ?

If these standard Plots.jl arguments do not work, I do not know nothing about `Shapefile.jl` objects and associated Plots’ recipes.

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 26, 2022, 1:01pm UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/5 "2022-05-26T13:01:06Z")

</div>

With `zfill` it does not plot anything (but gives no error message).

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 28, 2022, 5:32pm UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/6 "2022-05-28T17:32:39Z")

</div>

Here’s the full code for reproducibility and possible soltuions.

```
# Load modules
using CSV
using DataFrames
using Dates
using Shapefile
using ZipFile
using Plots
using ColorSchemes

# Create directorties
for dir in ["data", "downloads", "shapefiles"]
    path = joinpath(pwd(), dir)
    if !ispath(path)
        mkpath(path)
    end
end

# Download shapefiles
zip_url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2021_v1.zip" 
zip_loc = joinpath(pwd(), "downloads", split(zip_url, "/")[end])
if ~isfile(zip_loc)
	download(zip_url, zip_loc)
end

# Extract shape files
r = ZipFile.Reader(zip_loc)
for f in r.files
    file_name = split(f.name, "/")[end]
    if startswith(file_name, "gemeente")
        println("Extracting: $(file_name)")
        open(joinpath(pwd(), "shapefiles", file_name), "w") do io
            write(io, read(f))
        end
    end
end

# Read shapefiles
name_shapefile = "gemeente_2021_v1.shp"
path_shapefile = joinpath(pwd(), "shapefiles", name_shapefile)
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame
row_filter = df.H2O .== "NEE"
df = df[row_filter, [:geometry]]

# Add columns value1 and value2
df.value1 = rand(1:100, nrow(df))
df.value2 = rand(1:5000, nrow(df))

# Plot map
plot(df.geometry, palette = ColorSchemes.viridis, axis=false, ticks=false, size=(500, 600))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 28, 2022, 8:38pm UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/7 "2022-05-28T20:38:40Z")

</div>

I think you need to pass to the `plot` argument `fill_z` a transposed vector of values:  
` fill_z = permutedims(df.value1)`

Another possibility, would be to define a customized vector of colors `colorarray` and pass it as a fill color argument:  
`fc = permutedims(colorarray)`

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 28, 2022, 10:49pm UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/8 "2022-05-28T22:49:20Z")

</div>

Thanks! `fill_z = permutedims(df.value1)` works for me and does show a nice colorbar 😃

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 29, 2022, 10:06am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/9 "2022-05-29T10:06:36Z")

</div>

`fill_z = permutedims(df.value1)` does work, however, I does not seem to use the specified colorscheme. Any suggestion on how to fix that?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 29, 2022, 10:19am UTC](https://discourse.julialang.org/t/plotting-a-thematic-map-with-a-continuous-color-scale/81695/10 "2022-05-29T10:19:31Z")

</div>

I think you can use the `plot` argument `color` together with the palette of your choice. For example: `c=palette(:viridis)` or `c=palette(:inferno)`
