用单独的颜色填充Matplotlib Triplot中的三角形 [英] Filling triangles in Matplotlib triplot with individual colors

查看:75
本文介绍了用单独的颜色填充Matplotlib Triplot中的三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 pyplot 的 triplot 函数绘制由 scipy.spatial.Delaunay 生成的三角形列表,以便可以绘制每个三角形并用单独的颜色填充?我创建的基本 python 脚本是

Is it possible to plot a list of triangles generated by scipy.spatial.Delaunay using pyplot's triplot function so that each triangle can be drawn and filled with an individual color? The basic python script I've created is

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
import matplotlib.image as mpimg  

h = 300
w = 1000

npts = 30

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)

# Determine the color used for each triangle based upon the orthocenter
# of the triangle and the corresponding pixel color in a background image.

centers = np.sum(pts[tri.simplices], axis=1, dtype='int')/3.0
colors = [img[y,x] for x,y in centers]

# This plots the edges of each triangle with no fill. I'd like to 
# include the colors list to plot a fill for each.

plt.triplot(pts[:,0], pts[:,1], tri.simplices.copy())

plt.show()

triplot是否有一些参数,我可以在其中传递包含对应三角形颜色的颜色列表.我确信我可以使用适当的填充颜色在循环中绘制每个三角形,但如果有更优雅和更快的方法就好了.

Is there some argument to triplot where I can pass the colors list which contains the color for the corresponding triangle. I'm sure I can draw each triangle in a loop, using the appropriate fill color, but it be good if there was a more elegant and faster method.

推荐答案

您正在寻找的功能包含在 pyplot.tripcolor .

The functionality you're looking for is included in pyplot.tripcolor.

从其文档中,您将看到它是智能"的,并尝试猜测您是否为点或三角形指定了颜色:

From its documentation, you'll see that it is "smart" and tries to guess whether you have specified colors for the points or for the triangles:

下一个参数必须是 C,颜色值的数组,或者如果在处定义了颜色值,则三角剖分中的每个点一个点,或三角剖分中的每个三角形一个,如果颜色值在三角形处定义.如果分数相同和三角形中的三角形,假定颜色值是在点定义的;强制使用颜色值三角形使用kwarg的 facecolors = C而不是仅仅的 C .

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()

在这里,我仅基于三角形的中心与图像中心之间的距离设置颜色(因为我没有合适的图像).

Here I merely based the color on the distance between the center of the triangle and the center of the image (because I don't have a suitable image).

这篇关于用单独的颜色填充Matplotlib Triplot中的三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆