簡體   English   中英

三角剖分 Plot python 彎曲散點數據

[英]Triangulation Plot python curved scattered data

我正在嘗試從 matplotlib 獲取帶三角測量的插值輪廓曲面。我的數據看起來像一條曲線,我無法擺脫曲線下方的數據。 我想將外部數據點作為邊界。

我從本教程中獲得了代碼

import matplotlib.tri as tri
fig, (ax1, ax2) = plt.subplots(nrows=2)
xi = np.linspace(-10,150,2000)
yi = np.linspace(-10,60,2000)
triang = tri.Triangulation(x_after, y_after)

interpolator = tri.LinearTriInterpolator(triang, strain_after)
Xi, Yi = np.meshgrid(xi, yi)
zi = interpolator(Xi, Yi)

ax1.triplot(triang, 'ro-', lw=5)
ax1.contour(xi, yi, zi, levels=30, linewidths=0.5, colors='k')
cntr1 = ax1.contourf(xi, yi, zi, levels=30, cmap="jet")
fig.colorbar(cntr1, ax=ax1)
ax1.plot(x_after, y_after, 'ko', ms=3)

ax2.tricontour(x_after, y_after, strain_after, levels=30, linewidths=0.5, colors='k')
cntr2 = ax2.tricontourf(x_after, y_after, strain_after, levels=30, cmap="jet")
fig.colorbar(cntr2, ax=ax2)
ax2.plot(x_after, y_after, 'ko', ms=3)

plt.subplots_adjust(hspace=0.5)
plt.show()

在此處輸入圖像描述

我找到了使用此代碼屏蔽數據的選項,但我不知道如何定義屏蔽以獲得我想要的內容

triang.set_mask()

這些是內部曲線的值:

    x_after y_after z_after strain_after
39  117.2757    8.7586  0.1904  7.164
40  119.9474    7.152   0.1862  6.6456
37  111.8319    12.0568 0.1671  6.273
38  114.5314    10.4186 0.1651  5.7309
41  122.7482    5.4811  0.1617  9.1563
36  108.8823    13.4417 0.1421  8.8683
42  125.5035    3.8309  0.141   9.7385
33  99.8064 17.6315 0.1357  9.8613
32  96.8869 18.6449 0.1197  4.4147
35  105.8846    14.6086 0.1079  7.7055
28  84.2221 22.0191 0.1076  6.2098
26  77.8689 23.158  0.1067  7.5833
29  87.354  21.2974 0.1044  11.4365
27  81.0778 22.6443 0.1019  8.3794
24  71.4004 23.7749 0.0968  8.6207
34  102.8772    15.9558 0.0959  18.2025
23  68.2124 23.962  0.0939  7.9201
25  74.6905 23.4465 0.0901  9.0361
30  90.5282 20.398  0.0864  14.1051
31  93.802  19.335  0.0794  10.4563
43  128.3489    2.1002  0.0689  9.0292
22  65.0282 24.1107 0.0654  7.99
21  61.7857 24.0129 0.0543  8.2589
20  58.5831 23.9527 0.0407  9.0087
0   -0.0498 -0.5159 0.0308  7.1073
19  55.3115 23.7794 0.0251  9.6441
5   12.5674 9.3369  0.0203  7.2051
2   4.8147  3.6074  0.0191  8.0103
1   2.363   1.5329  0.0184  7.8285
18  52.0701 23.526  0.016   8.0149
3   7.4067  5.5988  0.0111  8.9994
7   18.2495 12.5836 0.0098  9.771
9   23.9992 15.4145 0.0098  6.7995
16  45.5954 22.5274 0.0098  12.9428
4   9.9776  7.5563  0.0093  6.9804
17  48.9177 23.0669 0.0084  9.3782
13  35.9812 20.0588 0.0066  9.6005
6   15.3578 11.0027 0.0062  9.7801
15  42.2909 21.8663 0.0052  12.0288
11  29.816  17.8723 0.0049  8.9085
8   21.1241 14.0893 0.0032  6.5716
10  26.8691 16.7093 0.0014  6.9672
44  131.1371    0.4155  0.0 11.9578
14  39.0687 20.991  -0.0008 9.9907
12  32.9645 18.9796 -0.0102 9.3389
45  134.083 -1.3928 -0.0616 15.29

在此處輸入圖像描述

我的大部分代碼都用於構建xy arrays 和節點編號方面的三角形列表,但我您已經擁有(或者至少您可以獲得)網格程序中的三角形列表你用過的……如果你有三角形,它就像plt.tricontourf(x, y, triangles, z)一樣簡單。

這是無聊內容的完整代碼。

import matplotlib.pyplot as plt
import numpy as np

th0 = th2 = np.linspace(-0.5, 0.5, 21)
th1 = np.linspace(-0.475, 0.475, 20)
r = np.array((30, 31, 32))

x = np.concatenate(( np.sin(th0)*r[0], 
                     np.sin(th1)*r[1],
                     np.sin(th2)*r[2]))
y = np.concatenate(( np.cos(th0)*r[0], 
                     np.cos(th1)*r[1],
                     np.cos(th2)*r[2]))

z = np.sin(x)-np.cos(y)

nodes0 = np.arange( 0, 21, dtype=int)
nodes1 = np.arange(21, 41, dtype=int)
nodes2 = np.arange(41, 62, dtype=int)

triangles = np.vstack((
    np.vstack((nodes0[:-1],nodes0[1:],nodes1)).T,
    np.vstack((nodes0[1:-1],nodes1[1:],nodes1[:-1])).T,
    np.vstack((nodes2[:-1],nodes1,nodes2[1:])).T,
    np.vstack((nodes1[:-1],nodes1[1:],nodes2[1:-1])).T,
    (0, 21, 41),
    (20, 61, 40)
    ))

fig, ax = plt.subplots()
ax.set_aspect(1)
tp = ax.triplot(x, y, triangles, color='k', lw=0.5, zorder=4)
tc = ax.tricontourf(x, y, triangles, np.sin(x)-np.cos(y))
plt.colorbar(tc, location='bottom')
plt.show()

我設法通過使用以下代碼找到一種方法來避免底部的三角形 plot:

xtri = x_after[triangles] - np.roll(x_after[triangles], 1, axis=1)
ytri = y_after[triangles] - np.roll(y_after[triangles], 1, axis=1)
maxi = np.max(np.sqrt(xtri**2 + ytri**2), axis=1)
max_radius = 4.5
triang.set_mask(maxi > max_radius)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM