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']
'''

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章