matplotlib: how to draw a rectangle on image

How to draw a rectangle on a image, like this: enter image description here

    import matplotlib.pyplot as plt
    from PIL import Image
    import numpy as np
    im = np.array('dog.png'), dtype=np.uint8)

I don't know what to do the next.

You can add a Rectangle patch to the matplotlib Axes.

For example (using the image from the tutorial here):

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    from PIL import Image
    import numpy as np

    im = np.array('stinkbug.png'), dtype=np.uint8)

    # Create figure and axes
    fig,ax = plt.subplots(1)

    # Display the image

    # Create a Rectangle patch
    rect = patches.Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none')

    # Add the patch to the Axes

enter image description here


Back to homepage or read more recommendations: