Trying to plot two polygons on the same cartesian grid with a different color for each polygon

20 views Asked by At

Using matplotlib.patches to plot two trapezoids, with different edgecolors, in a single Cartesian grid using "path". Only one polygon is showing up.

I expect a second, reflected trapezoid in graph.

import docx
#os.startfile("C:\\Users\\barry\\Desktop\\PYTHON\\Worksheet Template1.docx")
doc = docx.Document("C:\\Users\\barry\\Desktop\\PYTHON\\Worksheet Template1.docx")
table = doc.add_table(rows=3, cols=2)
doc.save("PythonInsertTable.docx")

import matplotlib.pyplot as plt
#import numpy as np
from matplotlib.path import Path
import matplotlib.patches as patches
from matplotlib.ticker import MultipleLocator
from docx.shared import Inches


# Creates two polygons with certain vertices

verts1 = [(1,1),(1,3),(3,3),(3,-1),(1,1)]
codes1 = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path1 = Path(verts1, codes1)
patch1 = patches.PathPatch(path1, edgecolor = "green", facecolor = "white", lw=2, zorder = 2)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(patch1)


verts2 = [(-1,1),(-1,3),(-3,3),(-3,-1),(-1,1)]
codes2 = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path2 = Path(verts2, codes2)
patch2 = patches.PathPatch(path2, edgecolor = "red", facecolor = "white", lw=2, zorder = 1)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_patch(patch2)


#Sets x and y axes
ax.set_xlim(-8,8)
ax.set_ylim(-8,8)
plt.grid(True)

#Determines the size of the plot
plt.rcParams["figure.figsize"] = [5.00, 5.00]
plt.axhline()
plt.axvline()
ax.grid(which = "minor")
ax.minorticks_on()
ax.xaxis.set_minor_locator(MultipleLocator(1 / 1))
ax.yaxis.set_minor_locator(MultipleLocator(1 / 1))
ax.tick_params(which = "minor", bottom = False, left = False)

plt.show()
plt.savefig("fig1.png")

p = table. Rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('fig1.png',width=Inches(3.0), height=Inches(3.0))
p = table.rows[0].cells[0].add_paragraph()
r = p.add_run()
r.add_picture('fig1.png',width=Inches(3.0), height=Inches(3.0))
r.add_picture

I've appended the lists containing the vertices and the codes and I got two trapezoids, BUT the same edgecolor and I need two different edgecolors. The code below produces two polygons.

verts1 = []
codes1 = []

verts1 = [(1,1),(1,3),(3,3),(3,-1),(1,1)]
codes1 = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]

verts1 += [(-1,1),(-1,3),(-3,3),(-3,-1),(-1,1)]
codes1 += [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]


fig = plt.figure()
ax = fig.add_subplot(111)
path1 = Path(verts1, codes1)
patch1 = patches.PathPatch(path1, edgecolor = "green", facecolor = "white", lw=2, zorder = 1)
ax.add_patch(patch1)
0

There are 0 answers