using DataFrames
using Plots
using Shapefile
using ZipFile
# make directory downloads
downloads = joinpath(pwd(), "downloads")
if ~ispath(downloads)
mkpath(downloads)
end
# make directory shapefiles
shapefiles = joinpath(pwd(), "shapefiles")
if ~ispath(shapefiles)
mkpath(shapefiles)
end
# download shapefiles
url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2020_v1.zip"
name_zipfile = split(url, "/")[end]
path_zipfile = joinpath(pwd(), "downloads", name_zipfile)
if ~isfile(path_zipfile)
download(url, path_zipfile)
end
# extract shapefiles
path_shapefile = joinpath(pwd(), "shapefiles", "gemeente_2020_v1.shp")
if ~isfile(path_shapefile)
r = ZipFile.Reader(path_zipfile)
for file in r.files
open(joinpath(pwd(), "shapefiles", file.name), "w") do io
write(io, read(file))
end
end
end
# read shapefile
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame
# filter for land (i.e. not water)
row_filter = df.H2O .== "NEE"
# filter data and shapes
municipality_data = df[row_filter, :]
municipality_shape = Shapefile.shapes(table)[row_filter]
function normalize(array)
"""
Normalize array to values between 0 and 1
"""
return [(x - minimum(array))/(maximum(array) - minimum(array)) for x in array]
end
# select variable to plot
var = "BEV_DICHTH" # population density
# values to plot
values = municipality_data[:, var]
normalized_values = normalize(values)
# colors
colormap = :heat
colors = Array([cgrad(colormap)[value] for value in normalized_values])
# plot thematic map
p = plot(size=(500, 600), axis=false, ticks=false)
for i = 1:nrow(municipality_data)
plot!(municipality_shape[i], fill_z=[normalized_values[i] for x in 1:length(municipality_shape[i].points)])
end
p
The coloring is not nice but this displays a colorbar.
Hey @lungd! That works beautifully! However, the colors aren’t used anywhere in your above example - I tried a different colormap gist_yarg for example - and I cannot get the correct colormap. Any thoughts on what to do to get the right colormap to propagate?
using DataFrames
using Plots
using Shapefile
states = Shapefile.Table("cb_2018_us_state_5m.shp") |> DataFrame
skip = ["02", "15", "60", "66", "69", "72", "78"]
filter!(row -> !(row[:STATEFP] in skip), states)
sort!(states, [:GEOID])
n = size(states, 1)
plot(states.geometry, fill_z=rand(1, n), title="My title", cbar=true, color=:seismic)
Colors are chosen automatically based on the corresponding fill_z values from the provided color gradient (ColorSchemes · Plots) - in this case :seismic, the default is :inferno as in @lungd’s example. If you only provide colors as fillcolor without corresponding “z” values, Plots can not know how to arrange these colors in a colorbar.