python 用 matplotlib 的 patch 模块绘制矩形,画一所房子

软件信息:

 

0、import

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import  Rectangle

 

1 、绘图

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

# 绘图区的背景色
rectangle = ax.patch
rectangle.set(facecolor='lightskyblue')

#
rectangle1 = Rectangle(xy=(1, 0),    # The bottom and left rectangle coordinates
                      width=3,
                      height=3, 
                      fc='lightyellow',    # facecolor, white
                      ec='rosybrown',    # edgecolor, 玫瑰棕色
                     )

#
rectangle2 = Rectangle(xy=(1.5, 0.1),    # 矩形块左下角坐标
                       width=1,
                       height=1.5,
                       fc='w',    # facecolor
                       ec='rosybrown',    # edgecolor
                       hatch='|||'    # 填充纹理
                      )

# 窗户
rectangle3 = Rectangle(xy=(2.9, 1.7),    # 矩形左下角坐标
                       width=0.6,
                       height=0.6,
                       fc='w',
                       ec='rosybrown',
                      )

rectangle_list = [eval(f'rectangle{i}') for i in range(1, 4)]
# 添加形状
for rect in rectangle_list:
    ax.add_patch(rect)
    
# 屋顶
ax.fill([0.8, 2.5, 4.2], [2.8, 4.5, 2.8],
        fc='red',
        ec='grey',
        hatch='-/',        
       )

# 烟囱
ax.fill([3.5, 3.5, 3.6, 3.6], [3.5, 3.8, 3.8, 3.4],
        fc='rosybrown',
       )

# 窗格线
ax.plot([2.9, 3.5], [2.0, 2.0],    # 水平线
        [3.2, 3.2], [1.7, 2.3],    # 垂直线
        color='rosybrown'
       )

# 门把手
ax.plot(2.4, 0.7,
        'ko',
        ms=6
       )

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

# 显示图形
plt.show()

图形:

 

posted @ 2020-05-17 23:12  赏尔  阅读(2170)  评论(0编辑  收藏  举报