2d heatmap

This example illustrates matrix operations by making a 2D Gaussian plot and computing area under a curve and volume under a surface using the Julia language.

  • 2017-09-07, Jeff Fessler, University of Michigan
  • 2023-06-06 Julia 1.9.0

This page comes from a single Julia file: gauss2d.jl.

You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: gauss2d.ipynb, or open it in binder here: gauss2d.ipynb.

Setup

Add the Julia packages used in this demo. Change false to true in the following code block if you are using any of the following packages for the first time.

if false
    import Pkg
    Pkg.add([
        "InteractiveUtils"
        "LaTeXStrings"
        "MIRTjim"
        "Plots"
    ])
end

Tell Julia to use the following packages. Run Pkg.add() in the preceding code block first, if needed.

using InteractiveUtils: versioninfo
using LaTeXStrings
using MIRTjim: jim
using Plots: default, heatmap, savefig
default(labelfontsize=18, tickfontsize=12, titlefontsize=18)

Broadast

x = range(-2, 2, 101)
y = range(-1.1, 1.1, 103) # deliberately non-square
A = abs2.(x) .+ 30 * abs2.(y)' # a lot is happening here!
F = exp.(-A)
p1 = heatmap(x, y, F', color=:grays, aspect_ratio=:equal)
Example block output

Heatmap

Here is a fancy Julia way, now with labels:

p2 = heatmap(range(-2, 2, 101), range(-1.1, 1.1, 103),
    (x,y) -> exp(-(abs2(x) + 3*abs2(y))), color=:grays, clim=(0,1),
    aspect_ratio=:equal, xlabel=L"x", ylabel=L"y", title=L"f(x,y)")
Example block output

jim

The jim function from MIRTjim.jl has natural defaults.

p3 = jim(x, y, F; xlabel=L"x", ylabel=L"y", title=L"f(x,y)", clim=(0,1))
# savefig(p3, "plot_exp4.pdf")
Example block output

Area

Compute 1D integral $\int_0^3 x^2 \, \mathrm{d}x$ numerically.

f(x) = x^2 # parabola
x = range(0,3,2000) # sample points
w = diff(x) # "widths" of rectangles
Area = w' * f.(x[2:end])
9.00675450281419

Volume

2D integral $\int_0^3 \int_0^2 \exp(-x^2 - 3 y^2) \, \mathrm{d}x \, \mathrm{d}y$

f(x,y) = exp(-(x^2 + 3*y^2)) # gaussian bump function
x = range(0,3,2000) # sample points
y = range(0,2,1000) # sample points
w = diff(x) # "widths" of rectangles in x
u = diff(y) # "widths" of rectangles in y
F = f.(x[2:end], y[2:end]') # automatic broadcasting again!
Volume = w' * F * u
0.4521691575122915

Reproducibility

This page was generated with the following version of Julia:

using InteractiveUtils: versioninfo
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
11-element Vector{SubString{String}}:
 "Julia Version 1.11.2"
 "Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)"
 "Build Info:"
 "  Official https://julialang.org/ release"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 4 × AMD EPYC 7763 64-Core Processor"
 "  WORD_SIZE: 64"
 "  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)"
 "Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/book-la-demo/book-la-demo/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [aaaa29a8] Clustering v0.15.7
  [35d6a980] ColorSchemes v3.27.1
⌅ [3da002f7] ColorTypes v0.11.5
⌃ [c3611d14] ColorVectorSpace v0.10.0
⌅ [717857b8] DSP v0.7.10
  [72c85766] Demos v0.1.0 `~/work/book-la-demo/book-la-demo`
  [e30172f5] Documenter v1.8.0
  [4f61f5a4] FFTViews v0.3.2
  [7a1cc6ca] FFTW v1.8.0
  [587475ba] Flux v0.15.2
  [a09fc81d] ImageCore v0.10.5
  [71a99df6] ImagePhantoms v0.8.1
  [b964fa9f] LaTeXStrings v1.4.0
  [7031d0ef] LazyGrids v1.0.0
  [599c1a8e] LinearMapsAA v0.12.0
  [98b081ad] Literate v2.20.1
  [7035ae7a] MIRT v0.18.2
  [170b2178] MIRTjim v0.25.0
  [eb30cadb] MLDatasets v0.7.18
  [efe261a4] NFFT v0.13.5
  [6ef6ca0d] NMF v1.0.3
  [15e1cf62] NPZ v0.4.3
  [0b1bfda6] OneHotArrays v0.2.6
  [429524aa] Optim v1.10.0
  [91a5bcdd] Plots v1.40.9
  [f27b6e38] Polynomials v4.0.12
  [2913bbd2] StatsBase v0.34.4
  [d6d074c3] VideoIO v1.1.1
  [b77e0a4c] InteractiveUtils v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [44cfe95a] Pkg v1.11.0
  [9a3f8284] Random v1.11.0
Info Packages marked with ⌃ and ⌅ have new versions available. Those with ⌃ may be upgradable, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated`

This page was generated using Literate.jl.