matplotlib未构造的四边形而不是三角形

时间:2018-04-03 23:03:53

标签: matplotlib mesh triangulation quadrilaterals

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

对于每个面包含3个顶点的网格,我可以使用matplotlib.tri进行可视化(例如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()

3 vertices per face

- 之后注释的相关点的索引

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

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

显然尝试tri.Triangulation无效:

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

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

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

编辑:根据最小,完整和可验证的示例更改了问题

1 个答案:

答案 0 :(得分:1)

正如已经评论的那样,由于没有Quatrangulation或simiar,因此没有标准的方法可以绘制类似的图,如在tripplotlib中每个形状有四个点的triplot。 当然,您可以再次对网格进行三角测量,以获得每个四边形2个三角形。或者,您可以根据空间坐标绘制形状的PolyCollection。下面显示了后者,定义了一个quatplot函数,它将顶点的坐标和索引作为输入,并绘制那些轴的PolyCollection。

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()

enter image description here