matplotlib 非结构化四边形而不是三角形
matplotlib unstructered quadrilaterals instead of triangles
我有两个包含非结构化网格的 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()
-- 后面注释的相关点索引
但是如何可视化每个面包含 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 个顶点的内容。非常感谢任何帮助..
编辑:根据最小、完整且可验证的示例更改了问题
正如已经评论过的那样,由于没有四角剖分或类似剖分,因此没有标准的方法来绘制与 matplotlib 中每个形状有四个点的三重图类似的图。
当然,您可以再次对网格进行三角剖分以获得每个四边形 2 个三角形。或者,您可以绘制形状的 PolyCollection,给定它们在 space 中的坐标。下面显示了后者,定义了一个 quatplot
函数,该函数将顶点的坐标和索引作为输入并将这些坐标和索引绘制到轴上。
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()
我有两个包含非结构化网格的 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()
-- 后面注释的相关点索引
但是如何可视化每个面包含 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 个顶点的内容。非常感谢任何帮助..
编辑:根据最小、完整且可验证的示例更改了问题
正如已经评论过的那样,由于没有四角剖分或类似剖分,因此没有标准的方法来绘制与 matplotlib 中每个形状有四个点的三重图类似的图。
当然,您可以再次对网格进行三角剖分以获得每个四边形 2 个三角形。或者,您可以绘制形状的 PolyCollection,给定它们在 space 中的坐标。下面显示了后者,定义了一个 quatplot
函数,该函数将顶点的坐标和索引作为输入并将这些坐标和索引绘制到轴上。
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()