以下小机器人是我利用matplotlib中楔(xie)形Wedge,弧线Arc,椭圆Ellipse,矩形Rectangle, 绘制这个机器人没有用到圆Circle,圆的关键参数只有圆心坐标和半径是关键,参数可借鉴代码中其他图形

python画圆matplotlib Python画圆角矩形_Arc

 以下是实现上面机器人的代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.patches import Arc,Ellipse,Rectangle,Wedge#导入需要的包

fig,ax=plt.subplots(subplot_kw={'aspect':'equal'})#创建一个画布,并设定坐标轴变化相同
#机器人脚底的椭圆形影子shadow,参数有中心点的坐标、长轴长,短轴长,颜色,透明度alpha
shadow=Ellipse((2.5,0.5),4.2,0.5,color='silver',alpha=0.2)
#画一条直线,下面表示点(1,1)到点(4,1.3)
ax.plot([1,4],[1,1.3],color='k')
#绘制脚上的弧线,其中(2.5,1.1)为弧线的中心,3为圆弧的宽度,1为圆弧高度,theta1为圆弧起点处的
#角度,theta2为圆弧终点处的角度,color为颜色,lw为弧线宽度
base=Arc((2.5,1.1),3,1,angle=10,theta1=0,theta2=180,color='k',alpha=0.8,lw=2)
#绘制左脚,椭圆尚未旋转的时候是平躺的,angle表示逆时针旋转九十度
left_wheel=Ellipse((1,1),0.7,0.4,angle=95,color='blue')
right_wheel=Ellipse((4,1.3),0.7,0.4,angle=85,color='k')#绘制右脚
#绘制红色和sliver色的屁股
bottom_joinstyle1=Ellipse((2.5,2),1,0.3,facecolor='silver',edgecolor='w')
bottom_joinstyle2=Ellipse((2.5,1.7),1,0.3,facecolor='red',edgecolor='w')
#绘制左肩膀
left_joinstyle=Ellipse((1,5.75),0.5,0.25,angle=90,color='k')
#绘制左手肘,指定坐标,半径,从多少度逆时针转到多少度,圆缺少了一块组成了楔形
left_arm_joinstyle1=Wedge((0.3,4.55),0.1,0,360,color='k')
#绘制左手
left_arm_joinstyle2=Wedge((0,4.0),0.2,290,250,color='k')
#绘制右肩膀
right_joinstyle=Ellipse((4,5.75),0.5,0.25,angle=90,color='k')
#绘制右手肘
right_arm_joinstyle1=Wedge((4.3,6.95),0.1,0,360,color='k')
绘制右手,为了方便区别,右手设定为了红色
right_arm_joinstyle2=Wedge((4.3,7.45),0.2,110,70,color='red')
#绘制两层椭圆形组成的脖子
top_joinstyle1=Ellipse((2.5,6.2),0.5,0.2,facecolor='silver',edgecolor='w')
top_joinstyle2=Ellipse((2.5,6.3),0.5,0.2,facecolor='silver',edgecolor='w')
#绘制矩形的身子
body=Rectangle((1,2.1),3,4,color='steelblue')
#左胳膊的大臂和小臂使用plot绘制线段,将线段的宽度设定为4
left_arm1=ax.plot([0.3,0.875],[4.55,5.75],color='silver',lw=4)
left_arm2=ax.plot([0,0.3],[4.2,4.55],color='silver',lw=4)
#右胳膊的大臂和小臂使用plot绘制线段,将线段的宽度设定为4
right_arm1=ax.plot([4.125,4.3],[5.75,6.95],color='silver',lw=4)   
right_arm2=ax.plot([4.3,4.3],[6.95,7.25],color='silver',lw=4)
#使用一个横线和一个弧线组成头的边框
ax.plot([1,4],[6.4,6.4],color='steelblue')                           
head=Arc((2.5,6.4),3,2.5,angle=0,theta1=0,theta2=180,color='steelblue')
#绘制两个眼睛
left_eye=Wedge((2,7),0.4,0,360,color='gold',width=0.6)    
left_eye_center=Wedge((2,7),0.05,15,345,color='black')
right_eye=Wedge((3,7),0.4,0,360,color='gold',width=0.6)
right_eye_center=Wedge((3,7),0.05,15,345,color='black')                           
#将以上创建的图形对象全都放在polygon列表中
polygon=[shadow,
        base,
        left_wheel,
        right_wheel,
        bottom_joinstyle1,
        bottom_joinstyle2,
        left_joinstyle,
        left_arm_joinstyle1,
        left_arm_joinstyle2,
        right_joinstyle,
        right_arm_joinstyle1,
        right_arm_joinstyle2,
        top_joinstyle1,
        top_joinstyle2,
        body,
        head,
        left_eye,
        left_eye_center,
        right_eye,
        right_eye_center]
for pln in polygon:
   ax.add_patch(pln)#使用add_patch方法将创建的每一个图形对象添加到ax画布上
ax.axis([-0.5,6,-0.5,10])#指定画布坐标轴的范围
#使用text方法在画布上添加文字,指定坐标和确切的文字,颜色以及字体大小等
ax.text(0.5,8.5,' i am beautiful\n i am a robot',color='blue',fontsize=14)
ax.text(1.5,4,'The fairy',color='k',fontsize=12)
#自动调整在画板上创建的子图参数,使之填充整个图像区域。
plt.tight_layout()
plt.show()#展示图像