當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Matplotlib.axes.Axes.triplot()用法及代碼示例

Matplotlib是Python中的一個庫,它是數字的-NumPy庫的數學擴展。軸類包含大多數圖形元素:Axis,Tick,Line2D,Text,Polygon等,並設置坐標係。 Axes實例通過callbacks屬性支持回調。

matplotlib.axes.Axes.triplot()函數

matplotlib庫的axiss模塊中的Axes.triplot()函數還用於創建非結構化的三角形網格作為線和/或標記。

用法:


Axes.triplot(ax, *args, **kwargs)

參數:此方法接受以下描述的參數:

  • x, y:這些參數是要繪製的數據的x和y坐標。
  • triangulation:此參數是matplotlib.tri.Triangulation對象。
  • **kwargs:此參數是文本屬性,用於控製標簽的外觀。
  • 其餘所有args和kwargs與matplotlib.pyplot.plot()相同。

返回值:這將返回包含以下內容的2 Line2D的列表:

  • 為三角形邊繪製的線。
  • 為三角形節點繪製的標記

以下示例說明了matplotlib.axes中的matplotlib.axes.Axes.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, 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(1.5 * x) * np.cos(1.5 * y) 
    
fig, axs = plt.subplots() 
axs.tricontourf(triang, z) 
axs.triplot(triang, 'go-', color ='white') 
fig.tight_layout() 
    
axs.set_title('matplotlib.axes.Axes.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, 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) 
   
fig1, ax1 = plt.subplots() 
ax1.set_aspect('equal') 
ax1.triplot(triang, 'go-', lw = 2) 
ax1.set_title('matplotlib.axes.Axes.triplot() Example') 
plt.show()

輸出:




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 Matplotlib.axes.Axes.triplot() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。