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:如何绘制以节点为中心的二维不连续数据? 的相关文章

  • InterfaceError:连接已关闭(使用 django + celery + Scrapy)

    当我在 Celery 任务中使用 Scrapy 解析函数 有时可能需要 10 分钟 时 我得到了这个信息 我用 姜戈 1 6 5 django celery 3 1 16 芹菜 3 1 16 psycopg2 2 5 5 我也使用了psyc
  • 将字符串转换为带有毫秒和时区的日期时间 - Python

    我有以下 python 片段 from datetime import datetime timestamp 05 Jan 2015 17 47 59 000 0800 datetime object datetime strptime t
  • 如何使用固定的 pandas 数据框进行动态 matplotlib 绘图?

    我有一个名为的数据框benchmark returns and strategy returns 两者具有相同的时间跨度 我想找到一种方法以漂亮的动画风格绘制数据点 以便它显示逐渐加载的所有点 我知道有一个matplotlib animat
  • 如何生成给定范围内的回文数列表?

    假设范围是 1 X 120 这是我尝试过的 gt gt gt def isPalindrome s check if a number is a Palindrome s str s return s s 1 gt gt gt def ge
  • 如何打印没有类型的defaultdict变量?

    在下面的代码中 from collections import defaultdict confusion proba dict defaultdict float for i in xrange 10 confusion proba di
  • 如何在 Sublime Text 2 的 OSX 终端中显示构建结果

    我刚刚从 TextMate 切换到 Sublime Text 2 我非常喜欢它 让我困扰的一件事是默认的构建结果显示在 ST2 的底部 我的程序产生一些很长的结果 显示它的理想方式 如在 TM2 中 是并排查看它们 如何在 Mac 操作系统
  • pandas 替换多个值

    以下是示例数据框 gt gt gt df pd DataFrame a 1 1 1 2 2 b 11 22 33 44 55 gt gt gt df a b 0 1 11 1 1 22 2 1 33 3 2 44 4 3 55 现在我想根据
  • 打破嵌套循环[重复]

    这个问题在这里已经有答案了 有没有比抛出异常更简单的方法来打破嵌套循环 在Perl https en wikipedia org wiki Perl 您可以为每个循环指定标签 并且至少继续一个外循环 for x in range 10 fo
  • 安装后 Anaconda 提示损坏

    我刚刚安装张量流GPU创建单独的后环境按照以下指示here https github com antoniosehk keras tensorflow windows installation 但是 安装后当我关闭提示窗口并打开新航站楼弹出
  • 在 NumPy 中获取 ndarray 的索引和值

    我有一个 ndarrayA任意维数N 我想创建一个数组B元组 数组或列表 其中第一个N每个元组中的元素是索引 最后一个元素是该索引的值A 例如 A array 1 2 3 4 5 6 Then B 0 0 1 0 1 2 0 2 3 1 0
  • 使用 Pycharm 在 Windows 下启动应用程序时出现 UnicodeDecodeError

    问题是当我尝试启动应用程序 app py 时 我收到以下错误 UnicodeDecodeError utf 8 编解码器无法解码位置 5 中的字节 0xb3 起始字节无效 整个文件app py coding utf 8 from flask
  • Pandas Dataframe 中 bool 值的条件前向填充

    问题 如何转发 fill boolTruepandas 数据框中的值 如果是当天的第一个条目 True 到一天结束时 请参阅以下示例和所需的输出 Data import pandas as pd import numpy as np df
  • 循环中断打破tqdm

    下面的简单代码使用tqdm https github com tqdm tqdm在循环迭代时显示进度条 import tqdm for f in tqdm tqdm range 100000000 if f gt 100000000 4 b
  • Python:计算字典的重复值

    我有一本字典如下 dictA unit1 test1 alpha unit1 test2 beta unit2 test1 alpha unit2 test2 gamma unit3 test1 delta unit3 test2 gamm
  • 设置 torch.gather(...) 调用的结果

    我有一个形状为 n x m 的 2D pytorch 张量 我想使用索引列表来索引第二个维度 可以使用 torch gather 完成 然后然后还设置新值到索引的结果 Example data torch tensor 0 1 2 3 4
  • VSCode:调试配置中的 Python 路径无效

    对 Python 和 VSCode 以及 stackoverflow 非常陌生 直到最近 我已经使用了大约 3 个月 一切都很好 当尝试在调试器中运行任何基本的 Python 程序时 弹出窗口The Python path in your
  • 如何从没有结尾的管道中读取 python 中的 stdin

    当管道来自 打开 时 不知道正确的名称 我无法从 python 中的标准输入或管道读取数据 文件 我有作为例子管道测试 py import sys import time k 0 try for line in sys stdin k k
  • 在 Pandas DataFrame Python 中添加新列[重复]

    这个问题在这里已经有答案了 例如 我在 Pandas 中有数据框 Col1 Col2 A 1 B 2 C 3 现在 如果我想再添加一个名为 Col3 的列 并且该值基于 Col2 式中 如果Col2 gt 1 则Col3为0 否则为1 所以
  • 在 Python 类中动态定义实例字段

    我是 Python 新手 主要从事 Java 编程 我目前正在思考Python中的类是如何实例化的 我明白那个 init 就像Java中的构造函数 然而 有时 python 类没有 init 方法 在这种情况下我假设有一个默认构造函数 就像
  • 有效地绘制大时间序列(matplotlib)

    我正在尝试使用 matplotlib 在同一轴上绘制三个时间序列 每个时间序列有 10 6 个数据点 虽然生成图形没有问题 但 PDF 输出很大 在查看器中打开速度非常慢 除了以栅格化格式工作或仅绘制时间序列的子集之外 还有其他方法可以获得

随机推荐

  • 我们如何改变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 绘制这些数据 并在节点之间进行线性插值并正确表示沿每个面片面的不连续值