python画红色填充三角形_用单独的颜色填充Matplotlib三元组中的三角形

论坛 期权论坛 脚本     
匿名技术用户   2021-1-10 18:09   746   0

您要查找的功能包含在^{}中。在

从其文档中,您将看到它是“smart”,并尝试猜测您是否为点或三角形指定了颜色:The next argument must be C, the array of color values, either

one per point in the triangulation if color values are defined at

points, or one per triangle in the triangulation if color values

are defined at triangles. If there are the same number of points

and triangles in the triangulation it is assumed that color

values are defined at points; to force the use of color values at

triangles use the kwarg facecolors=C instead of just C.

继续你的例子:import numpy as np

import matplotlib.pyplot as plt

from scipy.spatial import Delaunay

h = 300

w = 1000

npts = 500

pts = np.zeros((npts,2))

pts[:,0] = np.random.randint(0,w,npts)

pts[:,1] = np.random.randint(0,h,npts)

tri = Delaunay(pts)

plt.xlim(0, w)

plt.ylim(0, h)

centers = np.sum(pts[tri.simplices], axis=1, dtype='int')/3.0

colors = np.array([ (x-w/2.)**2 + (y-h/2.)**2 for x,y in centers])

plt.tripcolor(pts[:,0], pts[:,1], tri.simplices.copy(), facecolors=colors, edgecolors='k')

plt.gca().set_aspect('equal')

plt.show()

这里我只是根据三角形中心和图像中心之间的距离来确定颜色(因为我没有合适的图像)。在

Aoo8G.png

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP