Matplotlib基础-几何图形

除了绘制各类分析图形(比如柱状图,折线图,饼图等等)以外,matplotlib 也可以在画布上任意绘制各类几何图形。
这对于计算机图形学、几何算法和计算机辅助设计等领域非常重要。

matplitlib 中的 patches 类提供了丰富的几何对象,
本篇抛砖引玉,介绍其中几种常用的几何图形绘制方法。

其实matplitlib封装的各种现成的图形对象(柱状图,折线图,饼图等等)本质也是基于 patches 来绘制的。

1. 多边形类

多边形可以用在地理信息图表中,用来表示地理区域的边界或地理要素的形状;
也可以用来展示数据的分布区域或边界,比如用在散点图中。

几种常用的多边形绘制方式如下:

1.1. 矩形

绘制矩形用Rectangle对象。

import matplotlib.pyplot as plt
import matplotlib.patches as mptch
fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
r1 = mptch.Rectangle(xy=(0.2, 0.3),
height=0.5,
width=0.7,
color="lightblue")
r2 = mptch.Rectangle(xy=(0.4, 0.6),
height=0.3,
width=0.2,
angle=60,
color="lightgreen")
ax.add_patch(r1)
ax.add_patch(r2)
plt.show()
import matplotlib.pyplot as plt
import matplotlib.patches as mptch






fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)

r1 = mptch.Rectangle(xy=(0.2, 0.3), 
                     height=0.5,
                     width=0.7,
                     color="lightblue")
r2 = mptch.Rectangle(xy=(0.4, 0.6),
                     height=0.3,
                     width=0.2,
                     angle=60,
                     color="lightgreen")

ax.add_patch(r1)
ax.add_patch(r2)
plt.show()
import matplotlib.pyplot as plt import matplotlib.patches as mptch fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) r1 = mptch.Rectangle(xy=(0.2, 0.3), height=0.5, width=0.7, color="lightblue") r2 = mptch.Rectangle(xy=(0.4, 0.6), height=0.3, width=0.2, angle=60, color="lightgreen") ax.add_patch(r1) ax.add_patch(r2) plt.show()

Rectangle对象的主要参数

  1. xy:矩形的起始点,也就是左下角的点
  2. height:矩形的高
  3. width:矩形的宽,高和宽设置一样就是正方形
  4. angle:沿着起始点,逆时针旋转的角度
  5. color:矩形的颜色,默认蓝色

代码运行效果
image.png

1.2. 任意多边形

任意多边形比较简单,给Polygon对象传入多边形的各个顶点即可。

fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
#三角形
p1 = mptch.Polygon(xy=[(0.1, 0.1), (0.4, 0.1), (0.3, 0.6)],
color='lightblue')
#四边形
p2 = mptch.Polygon(xy=[(0.5, 0.5), (0.9, 0.5), (0.9, 0.1), (0.6, 0.2)],
color='lightgreen')
ax.add_patch(p1)
ax.add_patch(p2)
plt.show()
fig = plt.figure(figsize=[6, 6])




ax = fig.add_subplot(111)










#三角形
p1 = mptch.Polygon(xy=[(0.1, 0.1), (0.4, 0.1), (0.3, 0.6)], 
                   color='lightblue')
#四边形
p2 = mptch.Polygon(xy=[(0.5, 0.5), (0.9, 0.5),  (0.9, 0.1), (0.6, 0.2)], 
                   color='lightgreen')
ax.add_patch(p1)
ax.add_patch(p2)

plt.show()
fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) #三角形 p1 = mptch.Polygon(xy=[(0.1, 0.1), (0.4, 0.1), (0.3, 0.6)], color='lightblue') #四边形 p2 = mptch.Polygon(xy=[(0.5, 0.5), (0.9, 0.5), (0.9, 0.1), (0.6, 0.2)], color='lightgreen') ax.add_patch(p1) ax.add_patch(p2) plt.show()

Polygon对象的主要参数

  1. xy:多边形各个顶点的列表
  2. color:多边形的颜色,默认蓝色

代码运行效果
image.png

1.3. 正多边形

虽然用Polygon对象也可以绘制正多边形,但是需要计算各个顶点的坐标位置,很麻烦。
matplotlib提供了专门绘制正多边形的对象CirclePolygon

fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
p1 = mptch.CirclePolygon(xy=(0.3, 0.3),
radius=0.2,
resolution=6,
color='lightgreen')
p2 = mptch.CirclePolygon(xy=(0.6, 0.6),
radius=0.2,
resolution=8,
color='lightblue')
ax.add_patch(p1)
ax.add_patch(p2)
plt.show()
fig = plt.figure(figsize=[6, 6])




ax = fig.add_subplot(111)










p1 = mptch.CirclePolygon(xy=(0.3, 0.3), 
                         radius=0.2, 
                         resolution=6, 
                         color='lightgreen')
p2 = mptch.CirclePolygon(xy=(0.6, 0.6), 
                         radius=0.2, 
                         resolution=8, 
                         color='lightblue')
ax.add_patch(p1)
ax.add_patch(p2)

plt.show()
fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) p1 = mptch.CirclePolygon(xy=(0.3, 0.3), radius=0.2, resolution=6, color='lightgreen') p2 = mptch.CirclePolygon(xy=(0.6, 0.6), radius=0.2, resolution=8, color='lightblue') ax.add_patch(p1) ax.add_patch(p2) plt.show()

CirclePolygon对象的主要参数

  1. xy:正多边形的中心点坐标
  2. radius:正多边形的外接圆半径
  3. resolution:正多边形的边数
  4. color:正多边形的颜色,默认蓝色

代码运行效果
image.png

2. 圆形类

圆形类也是使用比较多的形状。

2.1. 圆和椭圆

绘制圆和椭圆分别用CircleEllipse对象。

fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
c = mptch.Circle(xy=(0.5, 0.5),
radius=0.4,
color="lightblue")
e = mptch.Ellipse(xy=(0.5, 0.5),
height=0.3,
width=0.4,
color="lightgreen")
ax.add_patch(c)
ax.add_patch(e)
plt.show()
fig = plt.figure(figsize=[6, 6])




ax = fig.add_subplot(111)










c = mptch.Circle(xy=(0.5, 0.5), 
                 radius=0.4, 
                 color="lightblue")
e = mptch.Ellipse(xy=(0.5, 0.5), 
                  height=0.3, 
                  width=0.4, 
                  color="lightgreen")



ax.add_patch(c)
ax.add_patch(e)
plt.show()
fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) c = mptch.Circle(xy=(0.5, 0.5), radius=0.4, color="lightblue") e = mptch.Ellipse(xy=(0.5, 0.5), height=0.3, width=0.4, color="lightgreen") ax.add_patch(c) ax.add_patch(e) plt.show()

Circle对象的主要参数

  1. xy:圆心坐标
  2. radius:圆的半径
  3. color:圆的颜色,默认蓝色

Ellipse对象的主要参数

  1. xy:椭圆心坐标
  2. height:椭圆的高度
  3. width:椭圆的宽度
  4. color:椭圆的颜色,默认蓝色

**PS. **当椭圆的heightwidth设置一样时,椭圆就是圆了。

代码运行效果
image.png

3. 圆弧和扇形

除了完整的圆,也可以绘制弧形(Arc对象)和扇形(Wedge对象)。

fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
#圆弧
a = mptch.Arc(xy=(0.4, 0.7),
width=0.5, height=0.5,
angle=270,
theta1=0, theta2=120,
linewidth=10,
color="lightblue")
#扇形
w = mptch.Wedge(center=(0.3, 0.5),
r=0.2,
theta1=30, theta2=330,
color="lightgreen")
ax.add_patch(a)
ax.add_patch(w)
plt.show()
fig = plt.figure(figsize=[6, 6])




ax = fig.add_subplot(111)










#圆弧
a = mptch.Arc(xy=(0.4, 0.7), 
              width=0.5, height=0.5, 
              angle=270, 
              theta1=0, theta2=120, 
              linewidth=10, 
              color="lightblue")



#扇形
w = mptch.Wedge(center=(0.3, 0.5), 
                r=0.2, 
                theta1=30, theta2=330, 
                color="lightgreen")

ax.add_patch(a)
ax.add_patch(w)
plt.show()
fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) #圆弧 a = mptch.Arc(xy=(0.4, 0.7), width=0.5, height=0.5, angle=270, theta1=0, theta2=120, linewidth=10, color="lightblue") #扇形 w = mptch.Wedge(center=(0.3, 0.5), r=0.2, theta1=30, theta2=330, color="lightgreen") ax.add_patch(a) ax.add_patch(w) plt.show()

Arc对象的主要参数

  1. xy:圆弧的圆心坐标
  2. width:圆弧的宽度
  3. height:圆弧的高度
  4. angle:圆弧朝向的角度,逆时针旋转
  5. theta1:圆弧开始的角度,逆时针旋转
  6. theta2:圆弧结束的角度,逆时针旋转
  7. linewidth:圆弧的粗细
  8. color:圆弧的颜色,默认蓝色

PS. widthheight相等时,圆弧相当于是的一段,不相等时,圆弧相当于是椭圆的一段。
圆弧开始的角度其实就是 angle+theta1,结束的角度是angle+theta2

Wedge对象的主要参数

  1. xy:扇形的圆心坐标
  2. r:扇形的半径
  3. theta1:扇形开始的角度,逆时针旋转
  4. theta2:扇形结束的角度,逆时针旋转
  5. color:扇形的颜色,默认蓝色

代码运行效果:(本来想画个鱼钩钓鱼的抽象效果的,:))
image.png

4. 箭头

箭头也是一种比较常用的图形,可用来标注和指示数据的方向或关联性,比如增长或下降趋势;
还可以用来表示数据流向或关系。

matplotlib中用Arrow对象来绘制箭头。

fig = plt.figure(figsize=[6, 6])
ax = fig.add_subplot(111)
x, y, dx, dy = 0.1, 0.2, 0.2, 0.2
a1 = mptch.Arrow(x=x, y=y,
dx=dx, dy=dy,
width=0.2, color="lightblue")
a2 = mptch.Arrow(x=x+dx, y=y+dy,
dx=dx, dy=dy,
width=0.4, color="lightgreen")
ax.add_patch(a1)
ax.add_patch(a2)
plt.show()
fig = plt.figure(figsize=[6, 6])




ax = fig.add_subplot(111)










x, y, dx, dy  = 0.1, 0.2, 0.2, 0.2
a1 = mptch.Arrow(x=x, y=y, 
                 dx=dx, dy=dy, 
                 width=0.2, color="lightblue")
a2 = mptch.Arrow(x=x+dx, y=y+dy, 
                 dx=dx, dy=dy, 
                 width=0.4, color="lightgreen")



ax.add_patch(a1)
ax.add_patch(a2)
plt.show()
fig = plt.figure(figsize=[6, 6]) ax = fig.add_subplot(111) x, y, dx, dy = 0.1, 0.2, 0.2, 0.2 a1 = mptch.Arrow(x=x, y=y, dx=dx, dy=dy, width=0.2, color="lightblue") a2 = mptch.Arrow(x=x+dx, y=y+dy, dx=dx, dy=dy, width=0.4, color="lightgreen") ax.add_patch(a1) ax.add_patch(a2) plt.show()

Arrow对象的主要参数

  1. x:箭头的起点X坐标
  2. y:箭头的起点Y坐标
  3. dx:箭头的终点X坐标偏移起点X坐标的值
  4. dy:箭头的终点Y坐标偏移起点Y坐标的值
  5. width:箭头的宽度
  6. color:箭头的颜色,默认蓝色

PS. 其实就是起点坐标(x, y)终点坐标(x+dx, y+dy)

代码运行效果
image.png

5. 总结

几何图形是matplotlib最基本的能力,复杂花哨的分析图表归根结底都是这些基本的几何图形。

除了本篇介绍的这些,完整的patches可以参考官方文档:matplotlib.org/stable/api/…

image.png

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MYvjfP9l' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片