matplotlib基础4:常见图形绘制(matplotlib.patches模块对象)


# -*- coding: utf-8 -*-
'''
绘制常见图形对象如:Rectangle, ellipse, circle, polygon...
'''
import matplotlib.pyplot as plt
import matplotlib.patches as mp

plt.figure()

'''
矩形:mp/plt.Rectangle((左下方点坐标), 长, 宽, color=.., alpha=..)
     Return the left and bottom coords(坐标) of the rectangle
     --> return: Rectangle(xy=(0.4, 0.7), width=0.4, height=0.15, angle=0)
'''
rect = mp.Rectangle((0.4, 0.7), 0.4, 0.15, color='r', alpha=0.5, angle=-30)


'''
圆形:mp/plt.Circle((圆心坐标), 半径, color=.., angele=.., alpha=..)
    --> return: Circle(xy=(0.7, 0.4), radius=0.2)
'''
circ = plt.Circle((0.7, 0.4), 0.2, color='g', alpha=0.5)


'''
椭圆:matplotlib.Patches.Ellipse((圆心), 横轴, 竖轴, angele=.., color=.., fill=..)
    --> return: Ellipse(xy=(0.2, 0.8), width=0.2, height=0.3, angle=20)
'''
elli = mp.Ellipse((0.2, 0.8), 0.2, 0.3, angle=20, color='orange', fill=True)


'''
多边形:mp/plt.Polygon(([point-1], [point-2], [point-3], ...))
    --> return: Poly((0.15, 0.15) ...)
多边形顶点连接:point-1 --> point-2 --> point-3 --> point-4 --> point-1
'''
pgon = plt.Polygon(([0.15,0.15], [0.35, 0.4], [0.2, 0.6], [0.3, 0.2]))


# 添加常见的图形对象。这些对象被成为块(patch).完整的patch集合位于matplotlib.patches中
# 绘制patch对象图形:plt.gca().add_patch(patch_name)
plt.gca().add_patch(rect)
plt.gca().add_patch(circ)
plt.gca().add_patch(elli)
plt.gca().add_patch(pgon)

# 添加点的注释
plt.annotate('point-1', 
             xy=(0.15, 0.15), xycoords='data', 
             xytext=(-30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->',
                             connectionstyle='arc3, rad=.2')
             )
plt.annotate('point-2', 
             xy=(0.35, 0.4), xycoords='data', 
             xytext=(-30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->',
                             connectionstyle='arc3, rad=.2')
             )
plt.annotate('point-3', 
             xy=(0.2, 0.6), xycoords='data', 
             xytext=(-30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->',
                             connectionstyle='arc3, rad=.2')
             )
plt.annotate('point-4', 
             xy=(0.3, 0.2), xycoords='data', 
             xytext=(-30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->',
                             connectionstyle='arc3, rad=.2')
             )

plt.show()
plt.savefig('patches_sample.png', dpi=300, bbox_inches='tight')


'''
dir(matplotlib.patches)
Out[29]: 
['Arc',
 'Arrow',
 'ArrowStyle',
 'BoxStyle',
 'Circle',             # 圆
 'CirclePolygon',
 'ConnectionPatch',
 'ConnectionStyle',
 'Ellipse',            # 椭圆
 'FancyArrow',
 'FancyArrowPatch',
 'FancyBboxPatch',
 'Patch',
 'Path',
 'PathPatch',
 'Polygon',            # 多边形
 'Rectangle',          # 矩形
 'RegularPolygon',
 'Shadow',
 'Wedge',
 'YAArrow',
 '_Style',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_patch_alias_map',
 '_point_along_a_line',
 '_pprint_styles',
 '_pprint_table',
 '_simpleprint_styles',
 'absolute_import',
 'allow_rasterization',
 'artist',
 'bbox_artist',
 'cbook',
 'colors',
 'concatenate_paths',
 'division',
 'docstring',
 'draw_bbox',
 'get_cos_sin',
 'get_intersection',
 'get_parallels',
 'inside_circle',
 'k',
 'make_path_regular',
 'make_wedged_bezier2',
 'map',
 'math',
 'mlines',
 'mpl',
 'np',
 'patchdoc',
 'print_function',
 'six',
 'split_bezier_intersecting_with_closedpath',
 'split_path_inout',
 'transforms',
 'unicode_literals',
 'warnings',
 'zip']
'''

matplotlib基础4:常见图形绘制(matplotlib.patches模块对象)_第1张图片

你可能感兴趣的:(matplotlib)