Plotting a thematic map with a continuous color scale

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

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

Thanks for your reply. However, adding colorbar = true does not show a colorbar. And adding 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.

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

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)

# Download shapefiles
zip_url = "" 
zip_loc = joinpath(pwd(), "downloads", split(zip_url, "/")[end])
if ~isfile(zip_loc)
	download(zip_url, zip_loc)

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

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

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)

1 Like

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


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

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)