繁体   English   中英

Matplotlib填充切片之间

[英]Matplotlib fill between slices

我正在使用python创建3D打印机切片算法。 该算法使用numpy-STL将.stl加载到numpy数组中,然后计算一些水平面与网格三角形的交点。 我已经成功地计算了相交并将相交点存储在另一个numpy数组中(格式:[x1 y1 z1 x2 y2 z2 x3 y3 z3])。 然后,我为每个切片提取x向量和y向量并将其存储以进行绘图。

尝试可视化这些切片时出现问题。 如果我使用以下方法绘制散点图:

import matplotlib as plt
plt.scatter(x_vec,y_vec)

我得到: 环 正是我所期望的。

但是,当我尝试使用以下方法连接网段时:

plt.plot(x_vec,y_vec)

我得到了彼此之间不是局部的点之间的线的奇怪连接: 怪异的线 我要在此处切片的形状是一个简单的环。 直接检查散点图时,我看不到任何多余的点,并且在手动梳理点数据时,它们似乎也都正确排序了。

matplotlib如何连接最近的线是否有问题? 如果我使用fill_between那么对于哪些部分是内部部分以及哪些不是内部部分,会感到非常困惑。 如果查看3D堆叠的多个切片,mplot3D是否有解决方案? 我已经尝试过:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x_vec, y_vec, z_vec, color = 'b')

但是在3D模式下也是一样。 关于可视化这些切片有什么建议吗? 其他包装等?

我认为连接的线是切片器如何返回numpy数据的结果。 此函数采用(m,9)个三角形的数组并根据条件返回(1,3)(1,6)或(1,9)数组:

def get_intersects(tri_entry, layer_h):
    # Calculate intersections for current triangles
    # 4 Cases

    # Case 1: 3 vertices on plane
    if check_points(tri_entry[2::3], layer_h) == 3:
        return tri_entry

    # Case 2: 2 vertices on plane
    if check_points(tri_entry[2::3], layer_h) == 2:
        return coords_on_plane(tri_entry, layer_h)

    # Case 3: 1 vertex on plane
    if check_points(tri_entry[2::3], layer_h) == 1:
        # 2 Sub cases

        # (1) Other 2 points on opposited sides of slice
        if check_z(tri_entry, layer_h) == 0:
            intersect = vec_intersect(tri_entry, layer_h)
            return np.hstack((intersect, coords_on_plane(tri_entry, layer_h)))

        # (2) Other 2 points on same side of slice
        if check_z(tri_entry, layer_h) == 1:
            return coords_on_plane(tri_entry, layer_h)

    # Case 4: 0 vertices on plane
    if check_points(tri_entry[2::3], layer_h) == 0:
       # Check which lines interesct
       a =  vec_intersect(tri_entry[0:6], layer_h)
       b =  vec_intersect(tri_entry[3:9], layer_h)
       c =  vec_intersect(tri_entry[[0,1,2,6,7,8]], layer_h)
       intersect = np.hstack((a, b, c))
       intersect = list(filter(None.__ne__,  intersect))
       return np.asarray(intersect) 

试图传递所有x和y的向量化列表而不考虑点的依赖性是我做错的地方。 我尝试分别绘制每种情况,发现了这一点: 放大色

单点为红色,两个点为绿色,三个点为蓝色。

如果您已经可以使用三角形,则可能需要查看plt.tripcolor 由于您尚不清楚确切的数据格式,因此在此我无法提供进一步的帮助。

除此之外,您可以将数据点分别分为内圆和外圆的数据点,并为外值绘制某种颜色的填充多边形。 然后,使用背景色为内部值绘制一个多边形。 结果看起来像您在那儿响了。

import matplotlib.pyplot as plt
import numpy as np

phi1 = np.linspace(0,2*np.pi)
x1 = np.cos(phi1)
y1 = -np.sin(phi1)

phi2 = np.linspace(0.06,2*np.pi+0.06)
x2 = 0.9*np.cos(phi2)
y2 = -0.9*np.sin(phi2)

fig, ax=plt.subplots()

p1 = plt.Polygon(np.c_[x1,y1], color="lightskyblue")
ax.add_patch(p1)
p2 = plt.Polygon(np.c_[x2,y2], color="white")
ax.add_patch(p2)

ax.scatter(x1,y1, s=10, zorder=4)
ax.scatter(x2,y2, s=10, zorder=4)

plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM