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


Python Matplotlib.patches.PathPatch用法及代碼示例

Matplotlib是Python中令人驚歎的可視化庫,用於二維陣列圖。 Matplotlib是一個基於NumPy數組的多平台數據可視化庫,旨在與更廣泛的SciPy堆棧配合使用。

matplotlib.patches.PathPatch

matplotlib.patches.PathPatch用於繪製常規多曲線路徑補丁的類。

用法: class matplotlib.patches.PathPatch(path, **kwargs)


參數:

  • path:路徑是 matplotlib.path.Path Object 。

下表列出了有效的kwargs參數列表:

PROPERTY DESCRIPTION
agg_filter 一個過濾器函數,它使用一個(m,n,3)浮點數組和一個dpi值來返回一個(m,n,3)數組
alpha 浮點數或無
animated bool
抗鋸齒或抗鋸齒 unknown
capstyle {‘butt’,“回合”,‘projecting’}
clip_box Bbox
clip_on bool
clip_path [(Path,Transform)|補丁|無]
color rgba元組的顏色或順序
contains callable
edgecolor或ec或edgecolors 顏色或無或‘auto’
facecolor或fc或facecolors 顏色或無
figure figure
fill bool
gid str
hatch {‘/’、‘\’、‘|’、‘-’、‘+’、‘x’、‘o’、‘O’、‘.’、‘*’}
in_layout bool
joinstyle {‘miter’,“回合”,‘bevel’}
線型或ls {“-”,“-”,“-。”,“:”,“,(偏移量,on-off-seq),...}
線寬或線寬或lw 浮點數或無
path_effects AbstractPathEffect
picker 無或布爾或浮點數或可贖回
path_effects AbstractPathEffect
picker float或callable [[Artist,Event],Tuple [bool,dict]]
rasterized 布爾還是無
sketch_params (比例:浮點數,長度:浮點數,隨機性:浮點數)
snap 布爾還是無
transform matplotlib.transforms.Transform
url str
visible bool
zorder float

範例1:

import numpy as np 
import matplotlib.cm as cm 
import matplotlib.pyplot as plt 
import matplotlib.cbook as cbook 
from matplotlib.path import Path 
from matplotlib.patches import PathPatch 
  
  
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = np.exp(-X**2 - Y**2) 
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) 
Z = (Z1 - Z2) * 2
  
path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]]) 
patch = PathPatch(path, facecolor ='none') 
  
fig, ax = plt.subplots() 
ax.add_patch(patch) 
  
im = ax.imshow(Z, interpolation ='bilinear', cmap = cm.gray, 
               origin ='lower', extent =[-3, 3, -3, 3], 
               clip_path = patch, clip_on = True) 
im.set_clip_path(patch) 
  
plt.show()

輸出:

範例2:

import matplotlib.pyplot as plt  
import numpy as np 
from matplotlib.path import Path 
from matplotlib.patches import PathPatch 
  
  
fig = plt.figure()  
  
ax = fig.add_subplot(111, aspect ='equal')  
path = Path([[0, 0], [0, 1], [1, 0], [0, 0]]) 
patch = PathPatch(path, facecolor ='none') 
ax.add_patch(patch)  
  
Z, Z2 = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1)) 
  
im = plt.imshow(Z-Z2,  
                interpolation ='bilinear',  
                cmap = plt.cm.RdYlGn, 
                origin ='lower', 
                extent =[0, 1, 0, 1], 
                clip_path = patch, 
                clip_on = True) 
  
im.set_clip_path(patch) 
  
ax.set_xlim((0, 1))  
ax.set_ylim((0, 1))  
  
plt.show()

輸出:




相關用法


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