两种利用matplotlib绘制无填充的多边形的方法:ax.fill()和Polygon
下面我们将使用np.rand随机生成5个多边形的顶点,使用不同的方法绘制多边形。
ax.fill()绘制多边形
函数原型为:
Axes.fill(*args, data=None, **kwargs)
args参数指的是按x, y, [color]给出的x坐标列表、y坐标列表和多边形的颜色。
data参数是带标记的多边形参数,一般用不上
kwargs是Polygon类型的属性,一般也用不上
ax.fill
函数的调用方式为:
# x,y是元素数量相同的list
ax.fill(x, y) # a polygon with default color
ax.fill(x, y, "b") # a blue polygon
# x2,y2也是元素数量相同的list
ax.fill(x, y, x2, y2) # two polygons
ax.fill(x, y, "b", x2, y2, "r") # a blue and a red polygon
代码示例如下:
import numpy as np
import matplotlib.pyplot as plt
N=5# N个顶点
points1 = np.random.rand(N, 2)# 第一个多边形的顶点集
points2 = np.random.rand(N, 2)# 第二个多边形的顶点集
points3 = np.random.rand(N, 2)# 第三个多边形的顶点集
points4 = np.random.rand(N, 2)# 第四个多边形的顶点集
fig, ax1 = plt.subplots(1,1)
# 绘制
ax1.fill(points1[:,0],points1[:,1], facecolor='none', edgecolor='orangered', linewidth=2)
ax1.fill(points2[:,0],points2[:,1], facecolor='none', edgecolor='blue', linewidth=1)
ax1.fill(points3[:,0],points3[:,1], facecolor='none', edgecolor='black', linewidth=1)
ax1.fill(points4[:,0],points4[:,1], facecolor='none', edgecolor='green', linewidth=1)
# 显示
p