[英]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.