📜  Python中的 Matplotlib.pyplot.triplot()

📅  最后修改于: 2022-05-13 01:55:22.621000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.triplot()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 PyplotMatplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。

matplotlib.pyplot.triplot()函数:

matplotlib 库的 pyplot 模块中的 triplot ()函数用于绘制非结构化三角形网格作为线条和/或标记。

下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.triplot()函数:

示例 1:

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import numpy as np
   
# Create triangulation.
x = np.asarray([0, 1, 2, 3, 0.5, 1.5,
                2.5, 1, 2, 1.5])
  
y = np.asarray([0, 0, 0, 0, 1.0,
                1.0, 1.0, 2, 2, 3.0])
  
triangles = [[0, 1, 4], [1, 2, 5], 
             [2, 3, 6], [1, 5, 4], 
             [2, 6, 5], [4, 5, 7],
             [5, 6, 8], [5, 8, 7],
             [7, 8, 9]]
  
triang = mtri.Triangulation(x, y, triangles)
z = np.cos(1.5 * x) * np.cos(1.5 * y)
   
plt.tricontourf(triang, z)
plt.triplot(triang, 'go-')
   
plt.title('matplotlib.pyplot.triplot() Example')
plt.show()

输出:

示例 2:

# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
   
n_angles = 24
n_radii = 9
min_radius = 0.5
radii = np.linspace(min_radius, 0.9,
                    n_radii)
   
angles = np.linspace(0, 3 * np.pi, n_angles, 
                     endpoint = False)
  
angles = np.repeat(angles[..., np.newaxis], 
                   n_radii, axis = 1)
  
angles[:, 1::2] += np.pi / n_angles
   
x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
triang = tri.Triangulation(x, y)
   
triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1),
                         y[triang.triangles].mean(axis = 1))
                < min_radius)
  
plt.triplot(triang, 'go-', lw = 1)
plt.title('matplotlib.pyplot.triplot() Example')
plt.show()

输出: