mplx
Some useful extensions for Matplotlib.
Contour plots for functions with discontinuities
plt.contour |
mplx.contour(max_jump=1.0) |
Matplotlib has problems with contour plots of functions that have discontinuities. The software has no way to tell discontinuities and very sharp, but continuous cliffs apart, and contour lines will be drawn along the discontinuity.
mplx improves upon this by adding the parameter max_jump
. If the difference between two function values in the grid is larger than max_jump
, a discontinuity is assumed and no line is drawn. Similarly, min_jump
can be used to highlight the discontinuity.
As an example, take the function imag(log(Z))
for complex values Z. Matplotlib's contour lines along the negative real axis are wrong.
import matplotlib.pyplot as plt
import numpy as np
import mplx
x = np.linspace(-2.0, 2.0, 100)
y = np.linspace(-2.0, 2.0, 100)
X, Y = np.meshgrid(x, y)
Z = X + 1j * Y
vals = np.imag(np.log(Z))
# plt.contour(X, Y, vals, levels=[-2.0, -1.0, 0.0, 1.0, 2.0]) # draws wrong lines
mplx.contour(X, Y, vals, levels=[-2.0, -1.0, 0.0, 1.0, 2.0], max_jump=1.0)
mplx.contour(X, Y, vals, levels=[0.0], min_jump=1.0, linestyles=":")
plt.gca().set_aspect("equal")
plt.show()
Relevant discussions:
License
This software is published under the MIT license.