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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
