I am experimenting with matplotlib to draw numbers in numbers. Since squares are the most straightforward to draw, I started with them. In the end, I want to write a generator for polygons with a certain width. In this example, it will be a quadrangular polygon with right angles and a width of 1.
My current code shows the following, which is as expected, and almost at will.

Note that there is a line between 2,2 and 2,3 , which, I think, can be deleted if this is done using the correct algorithm instead of the current code.
The above summary is a square in a box in two boxes with an amplitude that increases with 1 , assuming that the big boxes are βbehindβ the rest of the blocks.
The method by which I wrote the code producing the above is, well, not really a function. This is a damn ugly collection of dots that resemble hollow squares.
import matplotlib.path as mpath import matplotlib.patches as mpatches import matplotlib.pyplot as plt fig, ax = plt.subplots() INNER_AMPLITUDE = 1.0 OUTER_AMPLITUDE = 3.0 Path_in = mpath.Path path_in_data = [ (Path_in.MOVETO, (INNER_AMPLITUDE, -INNER_AMPLITUDE)), (Path_in.LINETO, (-INNER_AMPLITUDE, -INNER_AMPLITUDE)), (Path_in.LINETO, (-INNER_AMPLITUDE, INNER_AMPLITUDE)), (Path_in.LINETO, (INNER_AMPLITUDE, INNER_AMPLITUDE)), (Path_in.CLOSEPOLY, (INNER_AMPLITUDE, -INNER_AMPLITUDE)), ] codes, verts = zip(*path_in_data) path_in = mpath.Path(verts, codes) patch_in = mpatches.PathPatch(path_in, facecolor='g', alpha=0.3) ax.add_patch(patch_in) x, y = zip(*path_in.vertices) line, = ax.plot(x, y, 'go-') Path_out = mpath.Path path_out_data = [ (Path_out.MOVETO, (OUTER_AMPLITUDE, -OUTER_AMPLITUDE)), (Path_out.LINETO, (-OUTER_AMPLITUDE, -OUTER_AMPLITUDE)), (Path_out.LINETO, (-OUTER_AMPLITUDE, OUTER_AMPLITUDE)), (Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE)), (Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)), (Path_out.LINETO, (-(OUTER_AMPLITUDE-INNER_AMPLITUDE), OUTER_AMPLITUDE-INNER_AMPLITUDE)), (Path_out.LINETO, (-(OUTER_AMPLITUDE-INNER_AMPLITUDE), -(OUTER_AMPLITUDE-INNER_AMPLITUDE))), (Path_out.LINETO, (OUTER_AMPLITUDE-INNER_AMPLITUDE, -(OUTER_AMPLITUDE-INNER_AMPLITUDE))), (Path_out.LINETO, (OUTER_AMPLITUDE-INNER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)), (Path_out.LINETO, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)), (Path_out.CLOSEPOLY, (OUTER_AMPLITUDE, OUTER_AMPLITUDE-INNER_AMPLITUDE)), ] codes, verts = zip(*path_out_data) path_out = mpath.Path(verts, codes) patch_out = mpatches.PathPatch(path_out, facecolor='r', alpha=0.3) ax.add_patch(patch_out) plt.title('Square in a square in a square') ax.grid() ax.axis('equal') plt.show()
Notice that I am considering this off-topic code review, as I am looking for an extension of my functionality, and not just rewriting that is in line with best practices. I feel like doing this completely wrong. First of all, first.
How to draw polygons with a specific width using matplotlib , assuming that the polygon will be surrounded on the outside by a strip of the same kind and at least the same width and completely filled inside?