matplotlib 无结构四边形而不是三角形 [英] matplotlib unstructered quadrilaterals instead of triangles

查看:82
本文介绍了matplotlib 无结构四边形而不是三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含非结构化网格的 netcdf 文件.第一个网格每面有3个顶点,第二个网格每面有4个顶点.

对于每个面包含3个顶点的网格,我可以使用 matplotlib.tri 进行可视化处理(例如

-相关点的索引随后被注释

但是如何可视化每个面(四边形)包含4个顶点的非结构化网格?在上一个例子之后,我的脸看起来像:

quatrang = np.asarray([[65、60、44、20]]

显然尝试 tri.Triangulation 不起作用:

quatr = tri.Triangulation(x, y, quatrang)ValueError: 三角形必须是 (?,3) 数组

我无法在 matplotlib 库中找到有关每面4个顶点的任何内容.非常感谢任何帮助..

根据一个最小,完整和可验证的示例更改了问题

解决方案

正如已经评论过的,由于没有 Quatrangulation 或 simiar,没有标准方法可以在 matplotlib 中绘制每个形状有四个点的类似三重图的图.当然,您可以再次对网格进行三角剖分以获得每个四边形 2 个三角形.或者,您可以绘制形状的 PolyCollection,给定它们在空间中的坐标.下面显示了后者,定义了一个 quatplot 函数,该函数将顶点的坐标和索引作为输入,并将它们的 PolyCollection 绘制到轴上.

 将matplotlib.pyplot导入为plt将numpy导入为np导入matplotlib.collectionsxy = np.asarray([[-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],[-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],[-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],[-0.104,0.987],[-0.102、0.993],[-0.115、1.001],[-0.099、0.996],[-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],[-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],[ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],[ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],[-0.030,0.907],[-0.007,0.905],[-0.057,0.916],[-0.025,0.933],[-0.077,0.990],[-0.059,0.993]])x = np.degrees(xy[:, 0])y = np.degrees(xy [:, 1])quatrang = np.asarray([[19,13,10,22],[35,7,3,28]])def quatplot(x,y,quatrangles,ax = None,** kwargs):如果不是斧头:ax = plt.gca()xy = np.c_ [x,y]verts=xy[四边形]pc = matplotlib.collections.PolyCollection(verts,** kwargs)ax.add_collection(pc)ax.autoscale()plt.figure()plt.gca().set_aspect('equal')quatplot(x,y, quatrang, ax=None, color="crimson", facecolor="None")plt.plot(x,y, 标记="o", ls="", 颜色="深红色")plt.title('用户指定的quatrangulation的quatplot')plt.xlabel('经度(度)')plt.ylabel('纬度(度)')对于枚举(np.degrees(xy))中的i,(xi,yi):plt.text(xi,yi,i, size=8)plt.show()

I've two netcdf files containing both unstructured grids. The first grid has 3 vertices per face and the second has 4 vertices per face.

For the grid containing 3 vertices per face I can use matplotlib.tri for visualization (like triplot_demo.py:

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
xy = np.asarray([
    [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
    [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
    [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
    [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
    [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
    [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
    [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
    [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
    [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
    [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
    [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
    [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
    [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
    [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
    [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
    [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
    [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
    [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
    [-0.077, 0.990], [-0.059, 0.993]])
x = np.degrees(xy[:, 0])
y = np.degrees(xy[:, 1])

triangles = np.asarray([
    [65, 44, 20],
    [65, 60, 44]])

triang = tri.Triangulation(x, y, triangles)

plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'go-', lw=1.0)
plt.title('triplot of user-specified triangulation')
plt.xlabel('Longitude (degrees)')
plt.ylabel('Latitude (degrees)')

plt.show()

-- indices of the related point annotated afterwards

BUT how to visualize the unstructured grid containing 4 vertices per face (quadrilaterals)? Following the previous exapmle, my faces looks like:

quatrang = np.asarray([
    [65, 60, 44, 20]])

Obviously trying tri.Triangulation doesn't work:

quatr = tri.Triangulation(x, y, quatrang)

ValueError: triangles must be a (?,3) array

I cannot find anything in the matplotlib libraries regarding 4 vertices per face. Any help is greatly appreciated..

EDIT: Changed the question based upon a minimal, complete and verifiable example

解决方案

As commented already, since there is no Quatrangulation or simiar, there is no standard way to plot a a similar plot as triplot with four points per shape in matplotlib. Of course you could triangulate your mesh again to obtain 2 triangles per quadrilateral. Or, you can plot a PolyCollection of the shapes, given their coordinates in space. The following shows the latter, defining a quatplot function which takes the coordinates and the indices of the vertices as input and draws a PolyCollection of those to the axes.

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

xy = np.asarray([
    [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
    [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
    [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
    [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
    [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
    [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
    [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
    [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
    [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
    [-0.077, 0.990], [-0.059, 0.993]])
x = np.degrees(xy[:, 0])
y = np.degrees(xy[:, 1])

quatrang = np.asarray([
    [19,13,10,22], [35,7,3,28]])

def quatplot(x,y, quatrangles, ax=None, **kwargs):
    if not ax: ax=plt.gca()
    xy = np.c_[x,y]
    verts=xy[quatrangles]
    pc = matplotlib.collections.PolyCollection(verts, **kwargs)
    ax.add_collection(pc)
    ax.autoscale()


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

quatplot(x,y, quatrang, ax=None, color="crimson", facecolor="None")
plt.plot(x,y, marker="o", ls="", color="crimson")

plt.title('quatplot of user-specified quatrangulation')
plt.xlabel('Longitude (degrees)')
plt.ylabel('Latitude (degrees)')

for i, (xi,yi) in enumerate(np.degrees(xy)):
    plt.text(xi,yi,i, size=8)

plt.show()

这篇关于matplotlib 无结构四边形而不是三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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