python – **凹面**非网格数据的matplotlib等高线/轮廓f
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – **凹面**非网格数据的matplotlib等高线/轮廓f,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3375字,纯文字阅读大概需要5分钟。
内容图文
![python – **凹面**非网格数据的matplotlib等高线/轮廓f](/upload/InfoBanner/zyjiaocheng/709/68abc891eead43d7a300fdc685f18b13.jpg)
我想做一个非网格三维数据(x,y,z)的matplotlib等高线或等高线图,它在x和y中以某种方式呈C形(见草图) – 因此围绕数据的封闭船体的一部分在x和y中是凹的.
通常我会通过首先对其进行插值来绘制非网格化3D数据
from matplotlib.mlab import griddata
griddata...
但是这会在数据的凹陷部分中产生伪影,使得凹陷部分被插值填充.
是否可以进行插值或轮廓/等高线图
数据的凹陷部分是否受到尊重?
解决方法:
按条件掩盖
下面是如何使用带有遮罩的tricontourf来获得凹形的示例,其中没有数据外部的插值部分.它依赖于根据条件屏蔽数据的能力.
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
# create some data
rawx = np.random.rand(500)
rawy = np.random.rand(len(rawx))
cond01 = (rawx-1)**2 + rawy**2 <=1
cond02 = (rawx-0.7)**2 + rawy**2 >0.3
x = 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 shape
triang0 = 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 condition
cond1 = (x2-1)**2 + y2**2 <=1
cond2 = (x2-0.7)**2 + (y2)**2 >0.3
mask = np.where(cond1 & cond2,0,1)
# apply masking
triang.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 plt
import matplotlib.tri as tri
import numpy as np
# create some data
rawx = np.random.rand(500)
rawy = np.random.rand(len(rawx))
cond01 = (rawx-1)**2 + rawy**2 <=1
cond02 = (rawx-0.7)**2 + rawy**2 >0.3
x = 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 shape
triang1 = 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)可能需要一些调整.
内容总结
以上是互联网集市为您收集整理的python – **凹面**非网格数据的matplotlib等高线/轮廓f全部内容,希望文章能够帮你解决python – **凹面**非网格数据的matplotlib等高线/轮廓f所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。