How Do I Add a Colorbar to a Choropleth Map?

I’ll crosspost my response in the Plots issue ([BUG] Colorbar Is Missing When Plotting Geometries · Issue #3368 · JuliaPlots/Plots.jl · GitHub). The intended way to do this is fill_z instead of fillcolor.

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)

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

3 Likes