移动三角形顺序以匹配颜色图 [英] Shifting the triangles order to match colormap

查看:53
本文介绍了移动三角形顺序以匹配颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是先前

This question is a sequel of a previous one but regarding this time the colormap and the order of the triangle. I want to interpolate experimental data over a surface so as to enable a continuous colormap with however the surface known only at its corner node. To interpolate, I put a canonical example which works quite well but fails on real data.

确实如以下示例所示,初始三角剖分会生成两个三角形,它们之间存在巨大差距,请参见第一张图片.插值完成后,效果不会更好,并且色图也会丢失,请参见.第二张图片.到目前为止,最好的方法是将z和y相交以从一开始就获得相邻的三角形,从而成功进行插值.但是,您可能会在第三张图片中注意到,表面倾斜 90°,这是正常的,因为我将 y 切换为 z,反之亦然.

Indeed as shown in the example below, the initial triangulation results in two triangles with a huge gap between them, cf first picture. When the interpolation is done, it doesn't get any better and the colormap is also lost, cf. second picture. The best so far is by interverting z and y to get adjacent triangles from the beginning which results in a successful interpolation. However as you might notice in the third picture, the surface is tilted by 90° which is normal since I switch y for z and vice-versa.

但是,当我使用 ax.plot_trisurf(new.x, new_z, new.y, **kwargs) 在 tri_surf 函数中切换回 y 和 z 时,颜色图不跟随,参见.图片4.

However when I switch back y and z in the tri_surf function with ax.plot_trisurf(new.x, new_z, new.y, **kwargs), the colormap doesn't follow, cf. picture 4.

我想以某种方式旋转颜色图,或者使用 triang = tri.Triangulation(new.x, new_z) 从内插三角形生成新三角形,但没有成功.因此,关于正确地对两个相邻三角形进行初始三角剖分的任何想法或提示,如第三张图片,但具有面向表面的正确性,最终颜色图与 Y 值成正比.

I thought of rotating the colormap in somehow or generate new triangles from the interpolated ones with triang = tri.Triangulation(new.x, new_z) but without any success. So any idea or hint about properly doing the initial triangulation with two adjacent triangles, as for the third picture, but with the surface oriented correclty and ultimately the colormap proportional to the Y-value.

import numpy
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.tri as tri

x=numpy.array([0.00498316, 0.00498316, 0.00996632, 0.00996632])
y=numpy.array([-0.00037677, -0.00027191, -0.00078681, -0.00088475])
z=numpy.array([0., -0.0049926, 0., -0.00744763])

# Initial Triangle    
fig = plt.figure()
ax = Axes3D(fig)
triang = tri.Triangulation(x, y)

norm = plt.Normalize(vmax=y.max(), vmin=y.min())
ax.plot_trisurf(x, y, z, triangles=triang.triangles)

# Interpolated Triangle
fig = plt.figure()
ax = Axes3D(fig)
triang = tri.Triangulation(x, y)
refiner = tri.UniformTriRefiner(triang)
interpolator = tri.LinearTriInterpolator(triang, z)
new, new_z = refiner.refine_field(z, interpolator, subdiv=4)

kwargs = dict(triangles=new.triangles, cmap=cm.jet, norm=norm, linewidth=0,     antialiased=False)
ax.plot_trisurf(new.x, new.y, new_z, **kwargs)

# Best so far
fig = plt.figure()
ax = Axes3D(fig)
triang = tri.Triangulation(x, z)
refiner = tri.UniformTriRefiner(triang)
interpolator = tri.LinearTriInterpolator(triang, y)
new, new_z = refiner.refine_field(y, interpolator, subdiv=4)

kwargs = dict(triangles=new.triangles, cmap=cm.jet, norm=norm, linewidth=0, antialiased=False)
ax.plot_trisurf(new.x, new.y, new_z, **kwargs)

plt.show()

推荐答案

显然,自动三角剖分不会为您生成正确的三角形,但是您可以手动指定想要的三角形:

Apparently the automatic triangulation doesn't produce the right triangles for you, but you can specify how you want your triangles manually:

triang = tri.Triangulation(x, y, [[3,2,1],[1,2,0]])

# alternatively:
triang = tri.Triangulation(x, y, [[3,2,0],[1,3,0]])

这两种方式给出了截然不同的结果:

These two ways give rather different results:

然而,现在插值变得笨拙,因为对于某些 (x,y) 有多个 z 值.绕过这个问题的一种方法是分别插值和绘制 2 个大三角形:

However, now the interpolation becomes awkward, because for some (x,y) there are multiple z-values.. One way of bypassing this issue is interpolating and plotting the 2 large triangles separately:

import numpy
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.tri as tri


def plot_refined_tri(x, y, z, ax, subdiv=4, **kwargs):
    triang = tri.Triangulation(x, y)
    refiner = tri.UniformTriRefiner(triang)
    interpolator = tri.LinearTriInterpolator(triang, z)
    new, new_z = refiner.refine_field(z, interpolator, subdiv=subdiv)
    ax.plot_trisurf(new.x, new.y, new_z, triangles=new.triangles, **kwargs)


x=numpy.array([0.00498316, 0.00498316, 0.00996632, 0.00996632])
y=numpy.array([-0.00037677, -0.00027191, -0.00078681, -0.00088475])
z=numpy.array([0., -0.0049926, 0., -0.00744763])

fig = plt.figure()
ax = Axes3D(fig)
# note: I normalized on z-values to "fix" the colormap
norm = plt.Normalize(vmax=z.max(), vmin=z.min())
kwargs = kwargs = dict(linewidth=0.2, cmap=cm.jet, norm=norm)

idx = [3,2,1]
plot_refined_tri(x[idx], y[idx], z[idx], ax, **kwargs)

idx = [1,2,0]
plot_refined_tri(x[idx], y[idx], z[idx], ax, **kwargs)

plt.show()

结果:

这篇关于移动三角形顺序以匹配颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆