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


Python matplotlib.patches.Rectangle用法及代碼示例

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

matplotlib.patches.Rectangle

matplotlib.patches.Rectangle類別用於矩形貼圖到左下角xy =(x,y)並具有指定的寬度,高度和旋轉角度的圖。

用法: class matplotlib.patches.Rectangle(xy, width, height, angle=0.0, **kwargs)


參數:

  • xy:左下角開始繪製矩形
  • width:矩形的寬度
  • height:矩形的高度。
  • angle:矩形的旋轉角度。

下表列出了有效的kwarg;

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.pyplot as plt 
from matplotlib.patches import Rectangle 
  
  
# The image 
X = np.arange(16).reshape(4, 4) 
  
# highlight some feature in the 
# middle boxes. 
fig = plt.figure() 
  
ax = fig.add_subplot(111) 
ax.imshow(X, cmap = plt.cm.gray, 
          interpolation ='nearest') 
ax.add_patch( Rectangle((0.5, 0.5), 
                        2, 2, 
                        fc ='none',  
                        ec ='g', 
                        lw = 10) ) 
  
plt.show()

輸出:

範例2:

import matplotlib 
import matplotlib.pyplot as plt 
  
  
fig = plt.figure() 
ax = fig.add_subplot(111) 
  
rect1 = matplotlib.patches.Rectangle((-200, -100), 
                                     400, 200, 
                                     color ='green') 
  
rect2 = matplotlib.patches.Rectangle((0, 150), 
                                     300, 20, 
                                     color ='pink') 
  
rect3 = matplotlib.patches.Rectangle((-300, -50), 
                                     40, 200, 
                                     color ='yellow') 
  
ax.add_patch(rect1) 
ax.add_patch(rect2) 
ax.add_patch(rect3) 
  
plt.xlim([-400, 400]) 
plt.ylim([-400, 400]) 
  
plt.show()

輸出:




相關用法


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