How can I plot hysteresis threshold in Matplotlib?

To plot hysteresis threshold in Matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create a figure and a set of subplots.
  • Load some greek coins, Greek coins from Pompeii.
  • Find, high, low, and edges of the images using the sobel filter.
  • Apply hysteresis thresholding to "image".
  • Display the data as an image, i.e., on a 2D regular raster, using imshow() method.
  • Set the titles for the original image and the image with hysteresis threshold.
  • To display the figure, use show() method.


import matplotlib.pyplot as plt
from skimage import data, filters

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots(nrows=1, ncols=2)
orig_img = data.coins()
edges = filters.sobel(orig_img)

low = 0.1
high = 0.4
low = (edges > low).astype(int)
height = (edges > high).astype(int)
hyst = filters.apply_hysteresis_threshold(edges, low, high)

ax[0].imshow(height + hyst, cmap='magma')
ax[0].set_xlabel('Hysteresis threshold')
ax[1].imshow(orig_img, cmap='magma')
ax[1].set_xlabel('Original Image')