Home > Mobile >  Adding multiple rectangle patches using Matplotlib
Adding multiple rectangle patches using Matplotlib

Time:07-14

I want to add multiple rectangle patches (rect1, rect2, rect3)using ax.add_patch but it takes 2 positional arguments only. How can I implement this? The current and the expected outputs are presented.

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig,ax = plt.subplots(1)
rect1 = mpl.patches.Rectangle((0,800),50, 20, linewidth=1, edgecolor='black', facecolor='black')
rect2 = mpl.patches.Rectangle((200,800),50, 20, linewidth=1, edgecolor='black', facecolor='black')
rect3 = mpl.patches.Rectangle((400,800),50, 20, linewidth=1, edgecolor='black', facecolor='black')
ax.add_patch(rect1)
ax.set_xlim(left = 0, right = 1000)
ax.set_ylim(bottom = 0, top = 1000)
plt.show()

The current output is

enter image description here

The expected output is

enter image description here

CodePudding user response:

You need to also add rect2 and rect3 to your figure: ax.add_patch(rect2) and ax.add_patch(rect3). For now you only add rect1, hence the result.

  • Related