Skip to content Skip to sidebar Skip to footer

Why Do Matplotlib Contour Labels Make Contours Disappear?

The sample data is generated as follows, import matplotlib as mpl print(mpl.__version__) # 3.3.3 import matplotlib.pyplot as plt import numpy as np def f(x, y=0): return np.pi

Solution 1:

It is indeed a problem of the log axes, especially around the asymptote zero. However, why not defining the log axes before plotting, so matplotlib can take this into consideration when plotting?

import matplotlib as mpl
print(mpl.__version__) # 3.3.3import matplotlib.pyplot as plt
import numpy as np

deff(x, y=0):
    return np.piecewise(x, [x < 1, np.logical_and(1 <= x, x < 10), x >= 10], [lambda x: 0, lambda x: (x - 1) / 9 * 1000, lambda x: 1000])

x = np.logspace(-5, 5, 100)
y = np.logspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig, ax = plt.subplots(figsize=(5, 3), dpi=120)
ax.set_xscale('log')
ax.set_yscale('log')
cr = ax.contour(X, Y, Z, levels=3, colors='black')
ax.clabel(cr, inline=True, fontsize=8, fmt='%d')

plt.show()

Sample output: enter image description here

Post a Comment for "Why Do Matplotlib Contour Labels Make Contours Disappear?"