Python中的 Matplotlib.pyplot.tricontourf()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。
Matplotlib.pyplot.tricontourf()
matplotlib 库的 pyplot 模块中的tricontourf()函数用于在非结构化三角形网格上绘制轮廓。
Syntax: matplotlib.pyplot.tricontourf(\*args, \*\*kwargs)
Parameters: This method accept the following parameters that are described below:
- x, y: These parameter are the x and y coordinates of the data which is to be plot.
- triangulation: This parameter is a matplotlib.tri.Triangulation object.
- Z: This parameter is is the array of values to contour, one per point in the triangulation.
- **kwargs: This parameter is Text properties that is used to control the appearance of the labels.
All remaining args and kwargs are the same as for matplotlib.pyplot.plot().
Returns: This returns the list of 2 Line2D containing following:
- The lines plotted for triangles edges.
- The markers plotted for triangles nodes
注意:仅 tricontourf 关键字参数是抗锯齿的,这是一个布尔启用抗锯齿,用于非结构化三角形网格上的轮廓。
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.tricontourf()函数:
示例 1:
Python3
#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, 0, 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, 5, 4], [2, 6, 5],
[4, 5, 7], [5, 6, 8], [5, 8, 7],
[7, 8, 9], [1, 2, 5], [2, 3, 6]]
triang = mtri.Triangulation(x, y, triangles)
z = np.cos(2.5 * x*x) + np.sin(2.5 * x*x)
t = plt.tricontourf(triang, z)
plt.title('matplotlib.pyplot.tricontourf() Example\n',
fontsize=14, fontweight='bold')
plt.show()
Python3
#Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
n_angles = 60
n_radii = 10
min_radius = 0.35
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, np.pi, n_angles, endpoint = False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis = 1)
angles[:, 1::2] += np.pi / n_angles
x = (10 * radii * np.cos(angles)).flatten()
y = (10 * radii * np.sin(angles)).flatten()
z = (np.cos(4*(radii)**2) * np.sin((angles)**2)).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)
tcf = plt.tricontourf(triang, z)
plt.colorbar(tcf)
plt.title('matplotlib.pyplot.tricontourf() Example\n',
fontsize=14, fontweight='bold')
plt.show()
输出:
示例 2:
Python3
#Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
n_angles = 60
n_radii = 10
min_radius = 0.35
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, np.pi, n_angles, endpoint = False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis = 1)
angles[:, 1::2] += np.pi / n_angles
x = (10 * radii * np.cos(angles)).flatten()
y = (10 * radii * np.sin(angles)).flatten()
z = (np.cos(4*(radii)**2) * np.sin((angles)**2)).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)
tcf = plt.tricontourf(triang, z)
plt.colorbar(tcf)
plt.title('matplotlib.pyplot.tricontourf() Example\n',
fontsize=14, fontweight='bold')
plt.show()
输出: