在同一坐标系中绘制两个多边形,每个多边形不同颜色
使用matplotlib.patches在一个坐标系中绘制两个梯形,边框颜色不同,使用的是“路径”。但是,只有一个多边形显示出来。
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
我已经把包含顶点的列表和代码都加上了,结果得到了两个梯形,但它们的边框颜色是一样的,我需要两个不同的边框颜色。下面的代码可以生成两个多边形。
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 个回答
暂无回答