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

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

我对 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[:,'o')
plt.show()

感谢您的帮助!

解决方法

您希望在三角剖分的顶点(即点)上进行着色,因此您应该使用 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,"or")
    else:
        assert count >= 7
        plt.plot(tri.points[k,"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,tri.points[cond_le_5,"og")
plt.plot(tri.points[cond_eq_6,tri.points[cond_eq_6,"or")
plt.plot(tri.points[cond_ge_7,tri.points[cond_ge_7,"oy")
,

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[:,points[:,tri.simplices,linestyle='--')
    for filter,color in zip([num_neigh <= 5,num_neigh == 6,num_neigh >= 7],['lime','crimson','gold']):
        plt.scatter(points[filter,points[filter,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?