python - 如何根据色标给 voronoi 上色?以及每个单元格的面积

标签 python numpy physics voronoi

是否可以给 scipy.spatial.Voronoi 上色?图表? I know it is.

但现在我的目标是根据色标为每个单元格着色以表示物理量。

如下图(PRL 107, 155704 (2011)):

enter image description here

而且我还想知道是否可以计算每个单元格的面积,因为这是我想计算的数量

最佳答案

色阶:

实际上是 link您提供的代码提供了为 Voronoi 图着色所需的代码。为了给每个单元格分配一个代表物理量的颜色,您需要使用 Map values to colors in matplotlib 中所示的方法将该物理量的值映射到标准化颜色图。 .

例如,如果我想为每个单元格分配一种与数量“速度”相对应的颜色:

import numpy as np
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d

# generate data/speed values
points = np.random.uniform(size=[50, 2])
speed = np.random.uniform(low=0.0, high=5.0, size=50)

# generate Voronoi tessellation
vor = Voronoi(points)

# find min/max values for normalization
minima = min(speed)
maxima = max(speed)

# normalize chosen colormap
norm = mpl.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.Blues_r)

# plot Voronoi diagram, and fill finite regions with color mapped from speed value
voronoi_plot_2d(vor, show_points=True, show_vertices=False, s=1)
for r in range(len(vor.point_region)):
    region = vor.regions[vor.point_region[r]]
    if not -1 in region:
        polygon = [vor.vertices[i] for i in region]
        plt.fill(*zip(*polygon), color=mapper.to_rgba(speed[r]))
plt.show()

示例输出:

(Voronoi diagram) )

细胞面积:

scipy.spatial.Voronoi 允许您访问每个单元格的顶点,您可以订购并应用 shoelace formula .我还没有对输出进行足够的测试以了解 Voronoi 算法给出的顶点是否已经排序。但如果没有,您可以使用点积来获取每个顶点的向量与某个引用向量之间的角度,然后使用这些角度对顶点进行排序:

# ordering vertices
x_plus = np.array([1, 0]) # unit vector in i direction to measure angles from
    theta = np.zeros(len(vertices))
    for v_i in range(len(vertices)):
        ri = vertices[v_i]
        if ri[1]-self.r[1] >= 0: # angle from 0 to pi
            cosine = np.dot(ri-self.r, x_plus)/np.linalg.norm(ri-self.r)
            theta[v_i] = np.arccos(cosine)
        else: # angle from pi to 2pi
            cosine = np.dot(ri-self.r, x_plus)/np.linalg.norm(ri-self.r)
            theta[v_i] = 2*np.pi - np.arccos(cosine)

    order = np.argsort(theta) # returns array of indices that give sorted order of theta
    vertices_ordered = np.zeros(vertices.shape)
    for o_i in range(len(order)):
        vertices_ordered[o_i] = vertices[order[o_i]]

# compute the area of cell using ordered vertices (shoelace formula)
partial_sum = 0
for i in range(len(vertices_ordered)-1):
    partial_sum += vertices_ordered[i,0]*vertices_ordered[i+1,1] - vertices_ordered[i+1,0]*vertices_ordered[i,1]
    partial_sum += vertices_ordered[-1,0]*vertices_ordered[0,1] - vertices_ordered[0,0]*vertices_ordered[-1,1]
area = 0.5 * abs(partial_sum)

关于python - 如何根据色标给 voronoi 上色?以及每个单元格的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41244322/

相关文章:

java - jScience 中的单元;角速度的单位是什么?

ios - 保持节点可见

python - Pympler 总结似乎没有意义

python - 复杂的变形 Pandas

python - 在 64 位 Windows 上安装 NumPy 和 SciPy(使用 Pip)

python - 列表的哪些元素进入哪个直方图箱?

numpy - 已排序的 numpy 数组的交集

python - OpenAI Gym - 如何打造独热观察空间?

python - Pandas 无法读取特定的 Excel 工作表

c++ - Tilemap 2D 逼真的流体物理