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()

圖形:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM