在matplotlib中有一个子模块patches,提供了绘制各种多边形的功能,常用的多边形及其画法如下 1.圆形 用法如下 fig,ax = plt.subplots(figsize=(4,4)) patch = patches.Circle((0.5, 0.5), radius=0.2, transform=ax.
在matplotlib中有一个子模块patches, 提供了绘制各种多边形的功能,常用的多边形及其画法如下
1. 圆形
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Circle((0.5, 0.5), radius=0.2, transform=ax.transAxes)
>>> ax.add_artist(patch)
<matplotlib.patches.Circle object at 0x00E14EF8>
>>> plt.show()
输出结果如下
2. 椭圆
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Ellipse((0.5, 0.5), width=0.4, height= 0.2)
>>> ax.add_artist(patch)
<matplotlib.patches.Ellipse object at 0x090E49A0>
>>> plt.show()
输出结果如下
上述椭圆是有填充色的,没有填充的椭圆可以通过如下代码实现
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Arc((0.5, 0.5), width=0.4, height= 0.2)
>>> ax.add_artist(patch)
<matplotlib.patches.Arc object at 0x01305E68>
>>> plt.show()
输出结果如下
3. 箭头
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Arrow(x=0.2, y=0.2, dx=0.5, dy=0.6)
>>> ax.add_artist(patch)
<matplotlib.patches.Arrow object at 0x07C0D478>
>>> plt.show()
输出结果如下
4. 矩形
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Rectangle(xy=(0.2,0.2), width=0.5, height=0.4)
>>> ax.add_artist(patch)
<matplotlib.patches.Rectangle object at 0x00A9F4F0>
>>> plt.show()
输出结果如下
5. 多边形
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.Polygon(np.array(([0.2, 0.2], [0.4, 0.6], [0.6, 0.2])))
>>> ax.add_artist(patch)
<matplotlib.patches.Polygon object at 0x0A494F28>
>>> plt.show()
输出结果如下
6. 连线
用法如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> patch = patches.ConnectionPatch(xyA=(0.2, 0.2), xyB=(0.8, 0.8), coordsA=ax.transData, coordsB=ax.transData)
>>> ax.add_artist(patch)
<matplotlib.patches.ConnectionPatch object at 0x00A84E98>
>>> plt.show()
输出结果如下
除了简单的连线外,还可以通过ConnectionStyle设置连线的样式,比如设置成曲线,代码如下
>>> fig,ax = plt.subplots(figsize=(4,4))>>> style = patches.ConnectionStyle.Arc3(rad=0.2)
>>> patch = patches.ConnectionPatch(xyA=(0.2, 0.2), xyB=(0.8, 0.8), coordsA=ax.transData, coordsB=ax.transData,connectionstyle=style)
>>> ax.add_artist(patch)
<matplotlib.patches.ConnectionPatch object at 0x01314718>
>>> plt.show()
输出结果如下
7. 自定义多边形
通过子模块path可以自定义形状,用法如下
>>> from matplotlib.path import Path>>> fig, ax = plt.subplots(figsize=(4,4))
>>> verts = [(0.2, 0.2),(0.2, 0.6),(0.5,0.8),(0.8,0.6),(0.8,0.2)]
>>> codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO]
>>> path = Path(verts, codes)
>>> patch = patches.PathPatch(path)
>>> ax.add_artist(patch)
<matplotlib.patches.PathPatch object at 0x090C20B8>
>>> plt.show()
输出结果如下
对于单个多边形而言,通过add_artist方法添加到axes上,如果需要绘制多个多边形,可以通过绘制PatchCollection来一次性添加多个,用法如下
>>> from matplotlib.collections import PathCollection>>> fig,ax = plt.subplots(figsize=(4,4))
>>> patch1 = patches.Arc((0.5, 0.5), width=0.4, height= 0.2)
>>> patch2 = patches.Arrow(x=0.2, y=0.2, dx=0.5, dy=0.6)
>>> patch = []
>>> patch.append(patch1)
>>> patch.append(patch2)
>>> p = PatchCollection(patch)
>>> ax.add_collection(p)
<matplotlib.collections.PatchCollection object at 0x0916D088>
>>> plt.show()
输出结果如下
通过patches子模块,可以灵活的在图中添加各种多边形。
·end·
一个只分享干货的
生信公众号