Skip to content

Histogram ​

You can use any Makie compatible recipe in GeoMakie, and this includes histograms! Until we get the datashader recipe working, you can also consider this a replacement for that if you use it with the correct nbins.

We use the excellent FHist.jl to create the histogram.

julia
using GLMakie, GeoMakie
using FHist
import GLMakie: Point2d

First, we generate random points in a normal distribution:

julia
random_data = randn(Point2d, 100_000)
100000-element Vector{Point{2, Float64}}:
 [0.01088035222919297, 1.2371150772844652]
 [-0.8500974915355709, 1.397161841447706]
 [-0.2189210485625394, 0.022014157869787485]
 [0.043590233747860356, 1.1852643373064247]
 [1.320385784673507, -0.4544466008764751]
 [-0.9728730017133013, -0.03128592596287645]
 [-0.08571191599685225, -0.5245409468121859]
 [0.1543050570114007, -1.3379255087951123]
 [0.006530964419487026, -0.24477046022595164]
 [0.004783118253216429, 0.9728838835697986]
 ⋮
 [-0.34179884540255806, -0.4439034578250875]
 [1.1983712027170983, 0.04305567577846699]
 [0.9075845911916203, 1.2524584782540338]
 [-0.38138512270382213, 1.5308903441569213]
 [-0.08029386292288536, -0.728972791285736]
 [-2.3024119486709225, -0.23403890442831346]
 [1.1691543916586715, 0.716790497716919]
 [-2.121277110937678, -1.1230552831733436]
 [1.3458591116216427, -0.4625096069616285]

then, we rescale them to be within the lat/long bounds of the Earth:

julia
xmin, xmax = extrema(first, random_data)
ymin, ymax = extrema(last, random_data)
latlong_data = random_data .* (Point2d(1/(xmax - xmin), 1/(ymax - ymin)) * Point2d(360, 180),)
100000-element Vector{Point{2, Float64}}:
 [0.45791126654974856, 27.176025698971234]
 [-35.77726261429046, 30.69181421032352]
 [-9.213526594543625, 0.4835906788265501]
 [1.8345416328642938, 26.037007132285463]
 [55.569848683012324, -9.982945589296548]
 [-40.94440134128239, -0.687265997799927]
 [-3.60727770441265, -11.522726149309433]
 [6.494093445020997, -29.390554426138113]
 [0.27486262633066155, -5.376935775490453]
 [0.2013026500659541, 21.3715909760336]
 ⋮
 [-14.384970165174163, -9.75134164898942]
 [50.43473443447835, 0.9458151249846425]
 [38.196668719833944, 27.513078141961334]
 [-16.051001006379376, 33.62946269027566]
 [-3.3792531429763732, -16.01353315757598]
 [-96.89947065375269, -5.141192923833737]
 [49.205113676391804, 15.74592157545438]
 [-89.27621717670368, -24.67044480928779]
 [56.641920906442714, -10.160067721751737]

finally, we can create the histogram.

julia
h = Hist2D((first.(latlong_data), last.(latlong_data)); nbins = (360, 180))
-188.0 173.0 -90.0 91.0
  • edges: (-188.0:1.0:173.0, -90.0:1.0:91.0)
  • bin counts: [0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0]
  • maximum count: 32.0
  • total count: 100000.0

This is what the histogram looks like without any projection,

julia
plot(h)

It's simple to plot to GeoAxis:

julia
plot(h; axis = (; type = GeoAxis))

The projection can also be arbitrary!

julia
plot(h; axis = (; type = GeoAxis, dest = "+proj=tissot +lat_1=60 +lat_2=65"))


This page was generated using Literate.jl.