python – **凹面**非網格數據的matplotlib等高線/輪廓f

我想做一個非網格三維數據(x,y,z)的matplotlib等高線或等高線圖,它在x和y中以某種方式呈C形(見草圖) – 因此圍繞數據的封閉船體的一部分在x和y中是凹的.

通常我會通過首先對其進行插值來繪制非網格化3D數據

from matplotlib.mlab import griddatagriddata...

但是這會在數據的凹陷部分中產生偽影,使得凹陷部分被插值填充.

是否可以進行插值或輪廓/等高線圖

數據的凹陷部分是否受到尊重?

解決方法:

按條件掩蓋

下面是如何使用帶有遮罩的tricontourf來獲得凹形的示例,其中沒有數據外部的插值部分.它依賴于根據條件屏蔽數據的能力.

import matplotlib.pyplot as pltimport matplotlib.tri as triimport numpy as np# create some datarawx = np.random.rand(500)rawy = np.random.rand(len(rawx))cond01 = (rawx-1)**2 rawy**2 <=1cond02 = (rawx-0.7)**2 rawy**2 >0.3x = rawx[cond01 & cond02]y = rawy[cond01 & cond02]f = lambda x,y: np.sin(x*4) np.cos(y)z = f(x,y)# now, x,y are points within a partially concave shapetriang0 = tri.Triangulation(x, y)triang = tri.Triangulation(x, y)x2 = x[triang.triangles].mean(axis=1) y2 = y[triang.triangles].mean(axis=1)#note the very obscure mean command, which, if not present causes an error.#now we need some masking condition.# this is easy in this case where we generated the data according to the same conditioncond1 = (x2-1)**2 y2**2 <=1cond2 = (x2-0.7)**2 (y2)**2 >0.3mask = np.where(cond1 & cond2,0,1)# apply maskingtriang.set_mask(mask)fig, (ax, ax2) = plt.subplots(ncols=2, figsize=(6,3))ax.set_aspect("equal")ax2.set_aspect("equal")ax.tricontourf(triang0, z, cmap="Oranges")ax.scatter(x,y, s=3, color="k")ax2.tricontourf(triang, z, cmap="Oranges")ax2.scatter(x,y, s=3, color="k")ax.set_title("tricontourf without mask")ax2.set_title("tricontourf with mask")ax.set_xlim(0,1)ax.set_ylim(0,1)ax2.set_xlim(0,1)ax2.set_ylim(0,1)plt.show()

按最大邊長掩蔽

如果您無法訪問確切的條件,但在點之間具有最大邊長(距離),則以下將是一種解決方案.它會掩蓋所有三角形,其中至少一邊長于某個最大距離.如果點密度相當高,這可以很好地應用.

import matplotlib.pyplot as pltimport matplotlib.tri as triimport numpy as np# create some datarawx = np.random.rand(500)rawy = np.random.rand(len(rawx))cond01 = (rawx-1)**2 rawy**2 <=1cond02 = (rawx-0.7)**2 rawy**2 >0.3x = rawx[cond01 & cond02]y = rawy[cond01 & cond02]f = lambda x,y: np.sin(x*4) np.cos(y)z = f(x,y)# now, x,y are points within a partially concave shapetriang1 = tri.Triangulation(x, y)triang2 = tri.Triangulation(x, y)triang3 = tri.Triangulation(x, y)def apply_mask(triang, alpha=0.4): # Mask triangles with sidelength bigger some alpha triangles = triang.triangles # Mask off unwanted triangles. xtri = x[triangles] - np.roll(x[triangles], 1, axis=1) ytri = y[triangles] - np.roll(y[triangles], 1, axis=1) maxi = np.max(np.sqrt(xtri**2 ytri**2), axis=1) # apply masking triang.set_mask(maxi > alpha)apply_mask(triang2, alpha=0.1)apply_mask(triang3, alpha=0.3)fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(9,3))ax1.tricontourf(triang1, z, cmap="Oranges")ax1.scatter(x,y, s=3, color="k")ax2.tricontourf(triang2, z, cmap="Oranges")ax2.scatter(x,y, s=3, color="k")ax3.tricontourf(triang3, z, cmap="Oranges")ax3.scatter(x,y, s=3, color="k")ax1.set_title("tricontourf without mask")ax2.set_title("with mask (alpha=0.1)")ax3.set_title("with mask (alpha=0.3)")for ax in (ax1, ax2, ax3): ax.set(xlim=(0,1), ylim=(0,1), aspect="equal")plt.show()

可以看出,在這里找到正確的參數(alpha)可能需要一些調整.

來源:https://www.icode9.com/content-1-472151.html

免責聲明:本文僅代表文章作者的個人觀點,與本站無關。其原創性、真實性以及文中陳述文字和內容未經本站證實,對本文以及其中全部或者部分內容文字的真實性、完整性和原創性本站不作任何保證或承諾,請讀者僅作參考,并自行核實相關內容。

http://www.hqucmw.tw/style/images/nopic.gif
我要收藏
贊一個
踩一下
分享到
?
分享
評論
首頁
四川金7乐奖金设置