当我给出更多数据点时,matplotlib tricontourf ploblem

问题描述 投票:2回答:1

当我试图绘制压力时,我遇到了问题。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib.cm as cm

def plot(x_plot, y_plot, a_plot):
    x = np.array(x_plot)
    y = np.array(y_plot)
    a = np.array(a_plot)

    triang = mtri.Triangulation(x, y)
    refiner = mtri.UniformTriRefiner(triang)
    tri_refi, z_test_refi = refiner.refine_field(a, subdiv=4)

    plt.figure(figsize=(18, 9))
    plt.gca().set_aspect('equal')
    #     levels = np.arange(23.4, 23.7, 0.025)
    levels = np.linspace(a.min(), a.max(), num=1000)
    cmap = cm.get_cmap(name='jet')
    plt.tricontourf(tri_refi, z_test_refi, levels=levels, cmap=cmap)
    plt.scatter(x, y, c=a, cmap=cmap)
    plt.colorbar()

    plt.title('stress plot')
    plt.show()

首先,我只使用8点积分:

x = [2.3384750000000003, 3.671702, 0.3356813, 3.325298666666667, 2.660479, 1.3271675666666667, 1.6680919666666665, 0.6659845666666667]
y = [0.614176, 0.5590579999999999, 0.663329, 0.24002166666666666, 0.26821433333333333, 0.31229233333333334, 0.6367503333333334, 0.3250663333333333]
a = [2.572, 0.8214, 5.689, -0.8214, -2.572, -4.292, 4.292, -5.689]
plot(x, y, a)

version a

然后我尝试给出矩形边界的信息:

x = [2.3384750000000003, 1.983549, 3.018193, 2.013683, 3.671702, 3.978008, 4.018905, 0.3356813, 0.0, 0.0, 1.0070439, 3.325298666666667, 2.979695, 2.660479, 1.3271675666666667, 0.9909098, 1.6680919666666665, 0.6659845666666667]
y = [0.614176, -0.038322, 0.922264, 0.958586, 0.5590579999999999, -0.1229, 0.87781, 0.663329, 1.0, 0.0, 0.989987, 0.24002166666666666, -0.079299, 0.26821433333333333, 0.31229233333333334, -0.014787999999999999, 0.6367503333333334, 0.3250663333333333]
a = [2.572, 2.572, 2.572, 2.572, 0.8214, 0.8214, 0.8214, 5.689, 5.689, 5.689, 5.689, -0.8214, -0.8214, -2.572, -4.292, -4.292, 4.292, -5.689]
plot(x, y, a)

version b

我不知道如何解决它以及为什么会这样。我想要的数字是:

photoshoped version

我已经在第二个图中做了每个点的散点图,并且有正确但为什么颜色不是轮廓。

非常感谢你。

python matplotlib contourf
1个回答
2
投票

UniformTriRefiner返回的字段在附加点的情况下不能很好地插值。相反,它引入了新的最小值和最大值,其值比原始点大20倍。

下图显示了正在发生的事情。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib.cm as cm

def plot(x_plot, y_plot, a_plot, ax=None):
    if ax == None: ax = plt.gca()
    x = np.array(x_plot)
    y = np.array(y_plot)
    a = np.array(a_plot)

    triang = mtri.Triangulation(x, y)
    refiner = mtri.UniformTriRefiner(triang)
    tri_refi, z_test_refi = refiner.refine_field(a, subdiv=2)

    levels = np.linspace(z_test_refi.min(), z_test_refi.max(), num=100)
    cmap = cm.get_cmap(name='jet')

    tric = ax.tricontourf(tri_refi, z_test_refi, levels=levels, cmap=cmap)
    ax.scatter(x, y, c=a, cmap=cmap, vmin= z_test_refi.min(),vmax= z_test_refi.max())
    fig.colorbar(tric, ax=ax)

    ax.set_title('stress plot')

fig, (ax, ax2) = plt.subplots(nrows=2, sharey=True,sharex=True, subplot_kw={"aspect":"equal"} )

x = [2.3384750000000003, 3.671702, 0.3356813, 3.325298666666667, 2.660479, 1.3271675666666667, 1.6680919666666665, 0.6659845666666667]
y = [0.614176, 0.5590579999999999, 0.663329, 0.24002166666666666, 0.26821433333333333, 0.31229233333333334, 0.6367503333333334, 0.3250663333333333]
a = [2.572, 0.8214, 5.689, -0.8214, -2.572, -4.292, 4.292, -5.689]
plot(x, y, a, ax)


x = [2.3384750000000003, 1.983549, 3.018193, 2.013683, 3.671702, 3.978008, 4.018905, 0.3356813, 0.0, 0.0, 1.0070439, 3.325298666666667, 2.979695, 2.660479, 1.3271675666666667, 0.9909098, 1.6680919666666665, 0.6659845666666667]
y = [0.614176, -0.038322, 0.922264, 0.958586, 0.5590579999999999, -0.1229, 0.87781, 0.663329, 1.0, 0.0, 0.989987, 0.24002166666666666, -0.079299, 0.26821433333333333, 0.31229233333333334, -0.014787999999999999, 0.6367503333333334, 0.3250663333333333]
a = [2.572, 2.572, 2.572, 2.572, 0.8214, 0.8214, 0.8214, 5.689, 5.689, 5.689, 5.689, -0.8214, -0.8214, -2.572, -4.292, -4.292, 4.292, -5.689]
plot(x, y, a, ax2)

plt.show()

enter image description here

可以看出,“插值”字段的值大大超过原始值。 原因是默认情况下,UniformTriRefiner.refine_field使用三次插值(CubicTriInterpolator)。文件说明

插值基于三角网格的Clough-Tocher细分方案(为了使其更清晰,网格的每个三角形将被划分为3个子三角形,并且在每个子三角形上,插值函数是2的三次多项式)坐标)。该技术源于FEM(有限元法)分析;使用的元素是简化的Hsieh-Clough-Tocher(HCT)元素。它的形状函数在1中描述。组合的函数保证是C1-平滑的,即它是连续的并且它的一阶导数也是连续的(这很容易在三角形内部显示,但在穿过边缘时也是如此)。

在默认情况下(kind ='min_E'),插值最小化由HCT元素形状函数生成的函数空间上的曲率能量 - 具有强加值但在每个节点处具有任意导数。

虽然这确实非常技术性,但我强调了一些重要的部分,即插值是平滑的并且具有定义的导数。为了保证这种行为,当数据非常稀疏但幅度波动较大时,过冲是不可避免的。

这里,数据根本不适合三次插值。要么尝试获取更密集的数据,要么使用线性插值。

© www.soinside.com 2019 - 2024. All rights reserved.