python 用 matplotlib 中的 patches 模塊繪制圓形步驟詳解


版本說明:

 

0、import

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

 

1、繪制圓形

# 創建畫布
fig = plt.figure(figsize=(12, 8), facecolor='beige',    # 米黃色
 ) # 划分子區
axes = fig.subplots(nrows=2, ncols=2, ) # --------------------------------- subplot(221) ---------------------------------
ax = axes[0, 0] cicle = Circle(xy=(2, 2),    # 圓心坐標
               radius=2,    # 半徑
               fc='white',    # facecolor
               ec='cornflowerblue',    # 淺藍色,矢車菊藍
 ) ax.add_patch(p=cicle) circle.set(facecolor='yellow', alpha=0.5, hatch='/', ls='-.', ec='r' ) # 調整坐標軸刻度范圍
ax.set(xlim=(-1, 5), ylim=(-1, 5) ) # --------------------------------- subplot(222) ---------------------------------
ax = axes[0, 1] rectangle = ax.patch rectangle.set(facecolor='gold', alpha=0.7 ) circle = Circle(xy=(2, 2),    # 圓心
                radius=2,    # 半徑
 ) ax.add_patch(p=circle) circle.set(fc='red',    # facecolor
           ec='green',    # edgecolor,
           alpha=0.6, lw=2,    # line widht
 ) # 調整坐標軸
ax.set(xlim=(-1, 5), ylim=(-1, 5), aspect='equal'    # x 軸和 y 軸的刻度單位比率
 ) # --------------------------------- subplot(223) ---------------------------------
ax = axes[1, 0] rectangle = ax.patch rectangle.set(facecolor='palegreen',    # 淡綠色,蒼綠色
              alpha=1 ) circle = Circle(xy=(2, 2),    # 圓心
                radius=2,    # 半徑
 ) ax.add_patch(p=circle) circle.set(fc='red',    # facecolor
           ec='yellow',    # edgecolor,
           alpha=0.6, lw=5,    # line widht
 ) # 調整坐標軸
ax.axis('equal'    # x 軸和 y 軸的刻度單位比率
 ) # --------------------------------- subplot(224) ---------------------------------
ax = axes[1, 1] rectangle = ax.patch rectangle.set(facecolor='lightskyblue', alpha=1 ) circle = Circle(xy=(2, 2),    # 圓心
                radius=2,    # 半徑
 ) ax.add_patch(p=circle) circle.set(fc='white',    # facecolor
           ec='magenta',    # edgecolor, 品紅
           alpha=0.6, lw=5,    # line widht
 ) # 調整坐標軸
ax.axis([-1, 5, 1, 5])    # 坐標軸刻度范圍 [xmin, xmax, ymin, ymax]
ax.set(aspect='equal',    # x 軸和 y 軸的刻度單位比率
       yticks=range(-1, 6, 1)    # 刻度位置
 ) # 設置子區布局
plt.subplots_adjust(left=0.1, right=0.8 ) # 顯示圖形
plt.show()

 

圖形:

 


免責聲明!

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



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