python:如何绘制以节点为中心的二维不连续数据?

2024-01-12

我有一个二维数据和二维四边形网格,描述了细分为补丁的域。数据在每个网格节点处定义。数据中的不连续性存在于补丁边界处,即数据在同一位置处被多重定义。

如何使用 Python 绘制这些数据,并在节点之间进行线性插值并正确表示沿每个面片面的不连续值?

下面是三个示例元素或补丁,每个元素或补丁都有六个节点值。

节点位置和值数据可能存储在[Kx3x2]数组,其中 K 是元素数量。例如,

x = np.array( [
[ [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]  ],  #element 0
[ [1.0, 2.0], [1.0, 2.0], [1.0, 2.0]  ],  #element 1
[ [2.0, 3.0], [2.0, 3.0], [2.0, 3.0]  ],  #element 2
] )

y = np.array( [
[ [0.0, 0.0], [0.5, 0.5], [1.0, 1.0]  ],  #element 0
[ [0.0, 1.0], [0.5, 1.5], [1.0, 2.0]  ],  #element 1
[ [1.0, 1.0], [1.5, 1.5], [2.0, 2.0]  ],  #element 2
] )

z = np.array( [
[ [0.0, 0.5], [0.0, 0.8], [0.0, 1.0]  ],  #element 0
[ [0.3, 1.0], [0.6, 1.2], [0.8, 1.3]  ],  #element 1
[ [1.2, 1.5], [1.3, 1.4], [1.5, 1.7]  ],  #element 2
] )

我考虑过pyplot.imshow()。这无法同时考虑整个域并且仍然表示多值不连续节点。打电话可能有用imshow()分别为每个补丁。但是,我如何在同一轴上绘制每个补丁图像?imshow()对于非矩形补丁也是有问题的,这是我的一般情况。

我考虑过pyplot.pcolormesh(),但它似乎只适用于以细胞为中心的数据。


一种选择是通过对所有元素进行三角测量,然后使用 matplotlib 进行绘图tripcolor() http://matplotlib.org/api/axes_api.html?highlight=tri#matplotlib.axes.Axes.tripcolor我现在发现的功能。两个有用的演示是here http://matplotlib.org/examples/pylab_examples/tripcolor_demo.html and here http://matplotlib.org/examples/pylab_examples/triplot_demo.html?highlight=triangulation.

Auto-triangulation of my global domain can be problematic, but Delaunay triangulation of a single quadrilateral works very well: triangulation displayed for just the center element

I create a global triangulation by appending the triangulation of each element. This means shared nodes are actually duplicated in the position arrays and value arrays. This allows for the discontinuous data at element faces. triangulation displayed for all elements

Drawing with linear interpolation and discontinuities as desired can be achieved with the tripcolor() function, supplying the node locations, and values for each node. final solution pcolor

I was a little concerned how contour plotting might work, since element faces are no longer logically connected. tricontour() still works as expected. (shown here with triangulation overlaid) contour plot with triangulation overlaid

用以下代码重现:

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

x = np.array( [
[ [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]  ],  #element 0
[ [1.0, 2.0], [1.0, 2.0], [1.0, 2.0]  ],  #element 1
[ [2.0, 3.0], [2.0, 3.0], [2.0, 3.0]  ],  #element 2
] )

y = np.array( [
[ [0.0, 0.0], [0.5, 0.5], [1.0, 1.0]  ],  #element 0
[ [0.0, 1.0], [0.5, 1.5], [1.0, 2.0]  ],  #element 1
[ [1.0, 1.0], [1.5, 1.5], [2.0, 2.0]  ],  #element 2
] )

z = np.array( [
[ [0.0, 0.5], [0.0, 0.8], [0.0, 1.0]  ],  #element 0
[ [0.3, 1.0], [0.6, 1.2], [0.8, 1.3]  ],  #element 1
[ [1.2, 1.5], [1.3, 1.4], [1.5, 1.7]  ],  #element 2
] )



global_num_pts =  z.size
global_x = np.zeros( global_num_pts )
global_y = np.zeros( global_num_pts )
global_z = np.zeros( global_num_pts )
global_triang_list = list()

offset = 0;
num_triangles = 0;

#process triangulation element-by-element
for k in range(z.shape[0]):
    points_x = x[k,...].flatten()
    points_y = y[k,...].flatten()
    z_element = z[k,...].flatten()
    num_points_this_element = points_x.size

    #auto-generate Delauny triangulation for the element, which should be flawless due to quadrilateral element shape
    triang = tri.Triangulation(points_x, points_y)
    global_triang_list.append( triang.triangles + offset ) #offseting triangle indices by start index of this element

    #store results for this element in global triangulation arrays
    global_x[offset:(offset+num_points_this_element)] = points_x
    global_y[offset:(offset+num_points_this_element)] = points_y
    global_z[offset:(offset+num_points_this_element)] = z_element

    num_triangles += triang.triangles.shape[0]
    offset += num_points_this_element


#go back and turn all of the triangle indices into one global triangle array
offset = 0
global_triang = np.zeros( (num_triangles, 3) )
for t in global_triang_list:
    global_triang[ offset:(offset+t.shape[0] )] = t
    offset += t.shape[0]

plt.figure()
plt.gca().set_aspect('equal')

plt.tripcolor(global_x, global_y, global_triang, global_z, shading='gouraud' )
#plt.tricontour(global_x, global_y, global_triang, global_z )
#plt.triplot(global_x, global_y, global_triang, 'go-') #plot just the triangle mesh

plt.xlim((-0.25, 3.25))
plt.ylim((-0.25, 2.25))
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python:如何绘制以节点为中心的二维不连续数据? 的相关文章

随机推荐

  • 我们如何改变tableview标题的字体?

    我正在为 tabelView 使用一些背景颜色 并且样式已分组 各部分标题中的文本不清楚 因此我需要修改文本颜色 以便标题文本应该可见 我想知道我们可以更改标题文本的颜色和大小吗 添加 terente 的答案 UIView tableVie
  • 如何仅在过去 365 天使用 group by 对 pandas 数据框执行滚动求和

    尝试计算 p id 仅过去 365 天的滚动总和 创建一个包含此滚动总和的新列 具有新列的数据框应如下所示 Date p id points roll sum 2016 07 29 57 11 11 2016 08 01 57 9 20 2
  • ag-grid valueFormatter 函数的自定义参数

    我可以将自定义参数传递给 ag grid valueFormatter 函数吗 喜欢 valueFormatter PercentageFormatter params 10 如果是 那么需要传递什么作为第一个参数来获取单元格值 Funct
  • 包含多种 Java 类型的表上的 DynamoDBMapper

    我有一个 DynamoDB 表 其中包含不止一种类型的逻辑实体 我的表存储 员工 和 组织 并在两者之间创建多对多关系 我正在努力解决如何使用 DynamoDBMapper 对实体和表进行建模 特别是在尝试编写将返回员工和组织的查询时 在我
  • 如何从phonegap android插件返回数组或其他集合元素类型

    这是我在 java 插件中测试代码的一部分 我正在使用phonegap 2 7 public boolean execute String action JSONArray args CallbackContext callbackCont
  • Requirejs - 在加载 data-main 之前配置 require

    我们第一次使用 requirejs 我在构建依赖项时遇到了麻烦 我已将主 app js 文件定义为 index html 中的 data main 属性 但是 我有一个文件定义了所有需要的路径 垫片配置 并且我希望它在 app js 文件之
  • 我可以使用 git 的脚本化提交模板吗?

    我们正在处理票证 当我们在第一行的 git 提交消息中使用票证编号时 票证就会使用提交消息进行更新 为了简单起见 我们总是在带有提交号的分支上工作 现在我想看到一条提交消息 其中票号已被填写 这一定是可能的 因为分支已经在提交模板中 但在被
  • 高效的弦修剪

    我有一个String价值 我想要trim https doc rust lang org stable std string struct String html method trim它 我可以做类似的事情 let trimmed s t
  • 计算每列中空值的数量

    我遇到过一个数据库 其表太宽 600 列 即使在没有参数的情况下询问前 100 行也需要 4 秒 我想把这些桌子缩小一点 为了弄清楚哪些列可以最容易地移动到新表或完全删除 我想知道每列中有多少个空值 这应该告诉我哪些信息可能最不重要 我将如
  • 何时将我的项目拆分为多个 C 文件? (大型项目的良好实践)[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我现在正在做一个C语言的大项目 我正在做其中的特定部分 另一个是由其他人完成的 我想知道什么时候应该将我的项目拆分为多个c文件 以及编写的最佳实
  • 使用 XOM 在具有默认命名空间的 xml 上应用 xpath

    我有下面的 XML 其中包含默认名称空间
  • 从字典列表中获取最后更新的字典消息

    我正在尝试从数据流中获取实体的最新更新消息 数据以字典列表的形式出现 其中每个字典都是实体的更新消息 我只需要该实体的最新更新 我的输入是一个字典列表 输出需要是一个字典的字典 注意 仅长度更新 类别保持静态 我知道哪一个是最新更新 因为对
  • 芹菜与亚马逊 SQS

    我想用亚马逊SQS http aws amazon com sqs 作为经纪人支持Celery http celeryproject org SQS 传输实现Kombu https github com ask kombu Celery 依
  • 将 url 和 hash 与 Bootstrap ScrollSpy 一起使用

    我有一个基于 twitter bootstrap 的导航菜单栏 我想应用滚动间谍来突出显示 我使用普通的 php include 将菜单包含到多个页面中 因此我使用文件名加书签链接到文件 例如 products php foo 但滚动间谍希
  • Angular 2—更改组件选择器

    The Angular 2 文档 https angular io docs ts latest guide displaying data html假设要定义这样的组件 使用魔术字符串作为选择器名称 import Component fr
  • 查找另一个字段mongodb的不同值组

    我收集了这样的文件 id ObjectId 5c0685fd6afbd73b80f45338 page id 1234 category list football sport time broadcast 09 13 id ObjectI
  • IOS越狱如何拦截短信/短信

    我目前正在尝试编写一个应用程序来拦截文本消息并根据该消息的内容做出反应 我试图挂钩 receivedMessage struct CKSMSRecord message replace BOOL replaceCKSMSService 类中
  • HTML 标签正在转换

    我有以下代码片段来获取存储在数据库表中的 XML 数据的输出 ServletOutputStream os response getOutputStream String contentDisposition attachment file
  • Android NDK:为什么 ndk-build 不在 Eclipse 中生成 .so 文件和新的 libs 文件夹?

    我按照本教程的步骤进行操作 http mindtherobot com blog 452 android beginners ndk setup step by step http mindtherobot com blog 452 and
  • python:如何绘制以节点为中心的二维不连续数据?

    我有一个二维数据和二维四边形网格 描述了细分为补丁的域 数据在每个网格节点处定义 数据中的不连续性存在于补丁边界处 即数据在同一位置处被多重定义 如何使用 Python 绘制这些数据 并在节点之间进行线性插值并正确表示沿每个面片面的不连续值