Different behaviour of contour in Plots and GLMakie

When plotting a contour plot of a quadratic, behaviour is different for Plots and Makie, while Makie is correct. The motivation is to show an iteration of the Newton method.

MWE
using Plots, GLMakie
using LinearAlgebra

x1_grid = -1:0.02:1
x2_grid = -1:0.02:1
xk = [0.7, 0.5]

f(x) = (x[1]+1)^4 + x[1]*x[2] + (x[2]+1)^4
∇f(x) = [4(x[1]+1)^3+x[2], x[1]+4(x[2]+1)^3]
∇²f(x) = [12(x[1]+1)^2 1;1 12(x[2]+1)^2]
m(x) = f(xk)+dot(∇f(xk),x-xk) + 1/2*dot(x-xk, ∇²f(xk), x-xk)
x_kp1 = xk - ∇²f(xk) \ ∇f(xk)

m_grid = [m([x1, x2]) for x1=x1_grid, x2=x2_grid]

Plots.contour(x1_grid, x2_grid, m_grid)
Plots.scatter!([x_kp1[1]], [x_kp1[2]])

fig = Figure()
Axis(fig[1,1])
GLMakie.contour(x1_grid, x2_grid, m_grid)
GLMakie.scatter!([x_kp1[1]], [x_kp1[2]])

Makie correctly shows the minimizer in the center of the ellipses while the Plots package is off. Am I doing something wrong?

For Plots you will have to switch the coordinates:

Plots.scatter!([x_kp1[2]], [x_kp1[1]])

I know I can do that, but the plot is still wrong. The function has optimum at $≈(0.13, 0.0) and not on the y-axis. The question is, why does Plots do this?

When plotting grids or matrices in Plots.jl, you should feed the transpose, i.e.: m_grid'

1 Like

Link to a recent related thread on the orientation of heatmaps.

1 Like