matplotlib三角形网格画图

triangulation = mtri.Triangulation(x_list,y_list,triangles=triangles)#指定三角形画图
# triangulation = mtri.Triangulation(x_list,y_list)#不指定三角形,根据坐标按照Delaunay 三角剖分确定三角形
fig = plt.figure()
sub = fig.add_subplot(111)
sub.scatter(x_list,y_list,marker='.', color='k', s=50)#绘制坐标点
# for i in range(N):#在每个点旁边标号
#     sub.text(x[i]+0.02, y[i]+0.02,
#              '{:d}'.format(i), fontsize=12)
sub.triplot(triangulation, color='k')
#给每一个三角形添加颜色
triangles = np.array(triangulation.triangles)
    ntri = len(triangles)
    for i in range(ntri):
        vertices = np.zeros([3,2])
        for j in range(3):
            vertices[j,0] = x_list[triangles[i,j]]
            vertices[j,1] = y_list[triangles[i,j]]
        x_center = (x_list[triangles[i,0]]+x_list[triangles[i,1]]+x_list[triangles[i,2]])/3
        poly= Polygon(vertices, color=plt.cm.plasma(x_center))
        sub.add_patch(poly)
# sub.set_xlim([0,1])
# sub.set_ylim([0,1])
plt.show()

你可能感兴趣的:(python)