Home > Software design >  Create shape using hlines, vlines with matplotlib so that the exterior corners are complete
Create shape using hlines, vlines with matplotlib so that the exterior corners are complete

Time:11-01

Example code:

fig, ax = plt.subplots()

ax.vlines(x=0.6, ymin=0.2, ymax=0.8, linewidth=10)
ax.hlines(y=0.2, xmin=0.2, xmax=0.6, linewidth=10)
ax.hlines(y=0.8, xmin=.6, xmax=0.8, linewidth=10)
ax.vlines(x=0.8, ymin=0.8, ymax=1.1, linewidth=10)
ax.hlines(y=1.1, xmin=.5, xmax=0.8, linewidth=10)
ax.vlines(x=0.5, ymin=0.4, ymax=1.1, linewidth=10)
ax.hlines(y=.4, xmin=.2, xmax=0.5, linewidth=10)

Which produces:

enter image description here

In the corners are gaps though, whereas I would like these to be flush.

To be clear, the corners currently look like:

enter image description here

Whereas I would like them to look like:

enter image description here

# Edit

Side note - if there's just a rectangle to be created the following can be used:

import matplotlib.patches as pt 
fig, ax = plt.subplots() 
frame = pt.Rectangle((0.2,0.2),0.4,0.6, lw=10,facecolor='none', edgecolor='k') 
ax.add_patch(frame)

More info on the edit here:

  • line segments using round cap style

  • Related