python 用 matplotlib 中的 patch 模块绘制几何形状(圆,椭圆,圆弧,锲形块,矩形),组装一个机器人图

图形预览:

 

 0、import

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc, Circle, Ellipse, Rectangle, Wedge

 

1、绘图

# 创建画布
fig, ax = plt.subplots(nrows=1,
                       ncols=1,
                       subplot_kw=dict(aspect='equal',
                                       facecolor='whitesmoke'
                                      ),
                       figsize=(12, 8),
                       facecolor='sienna'
                      )

# --------------------------------- head, body ---------------------------------
# head
ax.plot([1, 4],
        [6.4, 6.4],
        c='steelblue'
       )

head = Arc(xy=(2.5, 6.4),    # 椭圆中心,(圆弧是椭圆的一部分而已)
           width=3,    # 长半轴
           height=2.5,    # 短半轴
           theta1=0,    # 圆弧起点处角度
           theta2=180,    # 圆弧终点处角度
           fc='w',    # 填充色
           ec='steelblue'    # 边框颜色
          )

# body
body = Rectangle(xy=(1, 2.1),    # 左下角坐标
                 width=3,
                 height=4,
                 fc='steelblue'
                )


# --------------------------------- eyes ---------------------------------
# eye_socket
left_eye_socket = Wedge(center=(2, 7),    # 锲形的中心
                        r=0.4,    # 半径
                        theta1=0,    # 起始角度
                        theta2=360,    # 终止角度
                        fc='gold'    # 填充颜色
                       )
right_eye_socket = Wedge(center=(3, 7),
                         r=0.4,
                         theta1=0,
                         theta2=360,
                         fc='k'
                       )

# eyeball
left_eyeball = Wedge(center=(2, 7),    # 中心
                     r=0.3,    # 半径
                     theta1=15,    # 起始角度
                     theta2=345,    # 终止角度
                     color='k'
                    )
right_eyeball = Wedge(center=(3, 7),    # 中心
                      r=0.3,    # 半径
                      theta1=156,    # 起始角度
                      theta2=195,    # 终止角度
                      color='darkred'
                     )

# --------------------------------- base, shadow ---------------------------------
# base
ax.plot([1.1, 4], [1, 1.3], color='k')
base = Arc(xy=(2.5, 1.1),    # 椭圆中心,(圆弧是椭圆的一部分而已)
           width=3,    # 长半轴
           height=1,    # 短半轴
           angle=10,    # 椭圆旋转角度(逆时针) 
           theta1=0,    # 圆弧的起点处角度
           theta2=175,    # 圆度的终点处角度
           color='k',
           alpha=0.8
          )

# shadow
shadow = Ellipse(xy=(2.5, 0.5),    # 椭圆中心
                 width=4.2,    # 长半轴
                 height=0.5,    # 短半轴
                 fc='silver',    # facecolor
                 alpha=0.2
                )

# --------------------------------- wheels ---------------------------------
left_wheel = Ellipse(xy=(1, 1),    # 中心
                     width=0.7,    # 长半轴
                     height=0.4,    # 短半轴
                     angle=95,    # 逆时针旋转角度
                     color='k'
                    )
right_wheel = Ellipse(xy=(4, 1.3),    # 中心
                      width=0.7,    # 长半轴
                      height=0.4,    # 短半轴
                      angle=85,    # 逆时针旋转角度
                      color='k'
                     )

# --------------------------------- arms ---------------------------------
left_upper_arm = ax.plot([0.3, 0.875],
                         [4.55, 5.75],
                         color='silver',    # facecolor
                         lw=4,    # line width
                        )
left_lower_arm = ax.plot([0, 0.3],
                         [4.2, 4.55],
                         color='silver',    # facecolor
                         lw=4,    # line width
                        )

right_upper_arm = ax.plot([4.125, 4.3],
                          [5.75, 6.95],
                          color='silver',    # facecolor
                          lw=4,    # line width
                         )
right_lower_arm = ax.plot([4.3, 4.3],
                          [6.95, 7.25],
                          color='silver',    # facecolor
                          lw=4,    # line width
                         )

# --------------------------------- hands ---------------------------------
left_hand = Wedge(center=(0, 4),
                  r=0.2,
                  theta1=290,
                  theta2=250,
                  fc='k'
                 )
right_hand = Wedge(center=(4.3, 7.45),
                   r=0.2,
                   theta1=110,
                   theta2=70,
                   fc='k'
                  )

# --------------------------------- shoulders ---------------------------------
left_shoulder = Ellipse(xy=(1, 5.75),    # 中心
                        width=0.5,    # 长半轴
                        height=0.25,    # 短半轴
                        angle=90,    # 逆时针旋转角度
                        fc='k',    # facecolor
                       )
right_shoulder = Ellipse(xy=(4, 5.75),    # 中心
                         width=0.5,    # 长半轴
                         height=0.25,    # 短半轴
                         angle=90,    # 逆时针旋转角度
                         fc='k',    # facecolor
                        )

# --------------------------------- elbows ---------------------------------
left_elbow = Wedge(center=(0.3, 4.55),
                   r=0.1,
                   theta1=0,
                   theta2=360,
                   fc='k'
                  )
right_elbow = Wedge(center=(4.3, 6.95),
                   r=0.1,
                   theta1=0,
                   theta2=360,
                   fc='k'
                  )

# --------------------------------- joints ---------------------------------
top_joint1 = Ellipse(xy=(2.5, 6.2),
                     width=0.5,
                     height=0.2,
                     fc='silver',
                     ec='w'
                    )
top_joint2 = Ellipse(xy=(2.5, 6.3),
                     width=0.5,
                     height=0.2,
                     fc='silver',
                     ec='w'
                    )

bottom_joint1 = Ellipse(xy=(2.5, 2),    # 中心
                        width=1,    # 长半轴
                        height=0.3,    # 短半轴
                        fc='silver',    # facecolor
                        ec='w'
                       )
bottom_joint2 = Ellipse(xy=(2.5, 1.7),    # 中心
                        width=1,    # 长半轴
                        height=0.3,    # 短半轴
                        fc='silver',    # facecolor
                        ec='w'
                       )

# --------------------------------- 向绘图区添加几何形状 ---------------------------------
polygons = [body,
            head,
            left_eye_socket,
            right_eye_socket,
            left_eyeball,
            right_eyeball,
            shadow,
            base,
            left_wheel,
            right_wheel,
            left_hand,
            right_hand,
            left_shoulder,
            right_shoulder,
            left_elbow,
            right_elbow,
            top_joint1,
            top_joint2,
            bottom_joint1,
            bottom_joint2
           ]

for pln in polygons:
    ax.add_patch(pln)


# 设置刻度范围
ax.axis([-1, 6, 0, 10])    # [xmin, xmax, ymin, ymax]

# 隐藏轴脊
for position in ['left', 'top', 'right', 'bottom']:
    ax.spines[position].set_visible(False)
    
# 隐藏刻度
ax.set(xticks=(),
       yticks=()
      )

# 显示图形
plt.show()

图形:

 

软件信息:

posted @ 2020-05-18 23:33  赏尔  阅读(2357)  评论(0编辑  收藏  举报