Skip to content

Instantly share code, notes, and snippets.

@shane5ul
Created April 2, 2021 18:58
Show Gist options
  • Save shane5ul/bd7e12b6bc0c7a8915f11e638bef5195 to your computer and use it in GitHub Desktop.
Save shane5ul/bd7e12b6bc0c7a8915f11e638bef5195 to your computer and use it in GitHub Desktop.
Matplotlib helpers to draw well-defined boxes on plots, and to sequentially connect a set of points with line segments.
def drawBox(xlim, ylim):
"""
provide xlim = [xmin, xmax] and ylim = [ymin, ymax]
returns vectors x, y, which when plotted draw a box connecting the endpoints
"""
pts = [[xlim[0], ylim[0]], [xlim[1], ylim[0]], [xlim[1], ylim[1]], [xlim[0], ylim[1]], [xlim[0], ylim[0]]]
x, y = zip(*pts)
return x, y
def connectPoints(pts):
"""pts = list of a bunch of points
returns two vectors x, y, which when plotted plot(x,y) joins the dots
eg. pts = [[0,0], [1, 0], [1, 1], [0,1], [0, 0]]"""
x, y = zip(*pts)
return x, y
# draw box
x, y = drawBox(xlim=[0,1], ylim=[0,1])
plt.plot(x,y)
# connect some points
x, y = connectPoints([[0.1, 0.2], [0.3, 0.3], [0.1, 0.3] ])
plt.plot(x,y)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment