Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding multiple rectangle patches using Matplotlib

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The expected output is

enter image description here

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading