matplotlib: how to draw a rectangle on image

前端 未结 4 1729
名媛妹妹
名媛妹妹 2020-11-28 02:01

How to draw a rectangle on an image, like this:

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
im = np.array(Image.open(\'dog.png\'         


        
4条回答
  •  遥遥无期
    2020-11-28 02:32

    You need use patches.

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    
    fig2 = plt.figure()
    ax2 = fig2.add_subplot(111, aspect='equal')
    
    ax2.add_patch(
         patches.Rectangle(
            (0.1, 0.1),
            0.5,
            0.5,
            fill=False      # remove background
         ) ) 
    fig2.savefig('rect2.png', dpi=90, bbox_inches='tight')
    

提交回复
热议问题