如何在Python中更改Delaunay点的颜色?

2024-06-28 10:53:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,我的任务是制作一个脚本,创建Delaunay三角剖分图。我已经成功地做到了这一点,但我需要根据图中有多少邻居来更改点的颜色。我有一个函数,它定义了邻居的数量,并将所有点放入一个字典中,以邻居的数量作为键。但是,我不知道如何将该字典应用于我的绘图-是否有一个特定的函数,您可以使用triplot创建的绘图并根据外部字典更改颜色?以下是我目前的代码:

from curved_analysis import read_xyz, read_nfo, num_neighbors
from scipy.spatial import Delaunay
import matplotlib.tri as mtri
import numpy as np
import matplotlib.pyplot as plt


coords = np.array(read_xyz("traj0.xyz"))
for k in range(coords.shape[0]):
    points = coords[k]
    tri = Delaunay(points[:, :2], qhull_options=('Qz'))




neigh = num_neighbors(tri)
for key in neigh:
    if key <=5:
        plt.triplot(neigh[key], color = 'green')
    if key == 6:
        plt.triplot(neigh[key], color = 'red')
    if key >= 7:
        plt.triplot(neigh[key], color = 'yellow')



plt.triplot(points[:,0], points[:,1], tri.simplices)
plt.plot(points[:,0], points[:,1], 'o')
plt.show()

谢谢你的帮助


Tags: keyimportreadif字典aspltcoords
2条回答

Numpy允许通过条件过滤点。例如points[num_neigh <= 5]将收集具有5个或更少邻居的所有点,前提是num_neigh是包含每个点的邻居数的numpy数组

num_neigh可以计算为tri.vertex_neighbor_vertices[0]中连续条目之间的差值,因此num_neigh = np.diff(tri.vertex_neighbor_vertices[0])

下面的代码使用plt.scatter绘制带有较大点的点。代码假定coords中的第一个维度表示某种层。注意,在Python中,建议直接使用元素而不是通过索引编写循环。问题不是很清楚,每一层是否应该是一个单独的地块,或者它们是否应该组合在子地块中

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

coords = np.random.uniform(0, 10, size=(1, 40, 2))
for points in coords:
    tri = Delaunay(points[:, :2], qhull_options=('Qz'))
    num_neigh = np.diff(tri.vertex_neighbor_vertices[0])

    plt.triplot(points[:, 0], points[:, 1], tri.simplices, linestyle=' ')
    for filter, color in zip([num_neigh <= 5, num_neigh == 6, num_neigh >= 7], ['lime', 'crimson', 'gold']):
        plt.scatter(points[filter, 0], points[filter, 1], s=80, color=color, zorder=2)
    plt.gca().set_aspect('equal', 'box') # equal axes needed because Delaunay depends on the scales
    plt.show()

example plot

您希望在三角剖分的顶点(即点)上进行着色,因此应使用plt.plot函数(替换最后一行之前的线)进行着色

下面是一种方法(继续编写代码)。用以下行替换上一行之前的行(上面的for key循环也应该被删除)

for k, count in neigh.items():
    if count <= 5:
        plt.plot(tri.points[k, 0], tri.points[k, 1], "og")
    elif count == 6:
        plt.plot(tri.points[k, 0], tri.points[k, 1], "or")
    else:
        assert count >= 7
        plt.plot(tri.points[k, 0], tri.points[k, 1], "oy")

通过随机抽取200个点进行三角测量,我得到下图:

enter image description here

注意:为了计算neigh字典,我使用了以下函数(您没有给出实现)。字典的关键是三角测量点中顶点的索引。还要注意的是,有更有效的方法可以做到这一点。例如,将计数存储在numpy数组而不是字典中(例如,使用np.diff(indptr)),并对每种情况使用矢量化操作,而不是在循环中逐个绘制

def num_neighbors(tri):
    indptr, indices = tri.vertex_neighbor_vertices
    assert len(indptr) == len(tri.points) + 1
    vertex_order_map = {}
    for k in range(len(indptr) - 1):
        num_neighbors_k = indptr[k+1] - indptr[k]
        vertex_order_map[k] = num_neighbors_k
    return vertex_order_map 

编辑:下面是更有效的矢量化代码。结果是相同的(直到点的着色顺序)

indptr, indices = tri.vertex_neighbor_vertices
neighbor_counter_array = np.diff(indptr)
cond_le_5 = (neighbor_counter_array <= 5)
cond_eq_6 = (neighbor_counter_array == 6)
cond_ge_7 = (neighbor_counter_array >= 7)
plt.plot(tri.points[cond_le_5, 0], tri.points[cond_le_5, 1], "og")
plt.plot(tri.points[cond_eq_6, 0], tri.points[cond_eq_6, 1], "or")
plt.plot(tri.points[cond_ge_7, 0], tri.points[cond_ge_7, 1], "oy")

相关问题 更多 >