python 用 matplotlib 的 patch 模塊繪制橢圓詳解


 

  

 0、import

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

 

1、繪制形狀

# 創建畫布
fig = plt.figure(figsize=(12, 8), facecolor='beige',    # 米黃色
 ) # 划分子區
axes = fig.subplots(nrows=2, ncols=3, subplot_kw={'aspect': 'equal'} ) # --------------------------------- subplot(221) ---------------------------------
ax = axes[0, 0] angles = np.linspace(0, 135, 4)    # 線型序列
ellipses = [Ellipse(xy=(2,2),    # 中心
                    width=4,    # 長半軸
                    height=2,    # 短半軸
                    angle=a,    # 旋轉角度(逆時針)
                   ) for a in angles ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=0.4, color='lightskyblue' ) ax.axis([-1, 5, -1, 5])    # 坐標軸刻度范圍 [xmin, xmax, ymin, ymax]

# --------------------------------- subplot(222) ---------------------------------
ax = axes[0, 1] angles = np.linspace(0, 180, 4, endpoint=False)    # 線型序列
ellipses = [Ellipse(xy=(2,2),    # 中心
                    width=4,    # 長半軸
                    height=2,    # 短半軸
                    angle=a,    # 旋轉角度(逆時針)
                   ) for a in angles ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=0.4, fc='r',    # facecolor, red
                ec='g',    # edgecolor, green
                lw=3,    # line width
 ) ax.axis([-1, 5, -1, 5])    # 坐標軸刻度范圍 [xmin, xmax, ymin, ymax]

# --------------------------------- subplot(223) ---------------------------------
ax = axes[0, 2] angles = np.linspace(0, 180, 4, endpoint=False)    # 線型序列
ellipses = [Ellipse(xy=(2,2),    # 中心
                    width=6,    # 長半軸
                    height=2,    # 短半軸
                    angle=a,    # 旋轉角度(逆時針)
                   ) for a in angles ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=0.5, fc='y',    # facecolor, red
                ec='r',    # edgecolor, green
                lw=3,    # line width
                ls=':',    # line style
 ) ax.set(xlim=(-1, 5),     # 坐標軸刻度范圍 (xmin, xmax)
       ylim=(-1, 5),    # ymin, ymax
       fc='green', ) # --------------------------------- subplot(224) ---------------------------------
ax = axes[1, 0] ellipses = [Ellipse(xy=np.random.rand(2)*10,    # 中心,在 0~1 上均勻分布的 1 個 1*2 的數組
                    width=np.random.rand(1),    # 長半軸, 在 0~1 上均勻分布的 1 個 float
                    height=np.random.rand(1),    # 短半軸
                    angle=np.random.rand(1)*360,    # 旋轉角度(逆時針)
                   ) for a in range(100) ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=np.random.rand(1)[0],    # alpha 需是 float
                color=np.random.rand(3)    # 3 元 RGB 序列,在 0~1 上均勻分布的 1 個 1*3 的數組
 ) ax.axis([-1, 11, -1, 11])    # 坐標軸刻度范圍 [xmin, xmax, ymin, ymax] 

# --------------------------------- subplot(225) ---------------------------------
ax = axes[1, 1] ellipses = [Ellipse(xy=np.random.rand(2)*10,    # 中心,在 0~1 上均勻分布的 1 個 1*2 的數組
                    width=np.random.rand(1),    # 長半軸, 在 0~1 上均勻分布的 1 個 float
                    height=np.random.rand(1),    # 短半軸
                    angle=np.random.rand(1)*360,    # 旋轉角度(逆時針)
                   ) for a in range(200) ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=np.random.rand(1)[0],    # alpha 需是 float
                fc=np.random.rand(3),    # 3 元 RGB 序列,在 0~1 上均勻分布的 1 個 1*3 的數組
                ec=np.random.rand(3), lw=2 ) ax.axis([-1, 11, -1, 11])    # 坐標軸刻度范圍 [xmin, xmax, ymin, ymax] 

# --------------------------------- subplot(226) ---------------------------------
ax = axes[1, 2] ellipses = [Ellipse(xy=np.random.rand(2)*10,    # 中心,在 0~1 上均勻分布的 1 個 1*2 的數組
                    width=np.random.rand(1),    # 長半軸, 在 0~1 上均勻分布的 1 個 float
                    height=np.random.rand(1),    # 短半軸
                    angle=np.random.rand(1)*360,    # 旋轉角度(逆時針)
                   ) for a in range(200) ] for ellipse in ellipses: ax.add_patch(p=ellipse)    # 向子區添加形狀
    ellipse.set(alpha=np.random.rand(1)[0],    # alpha 需是 float
                fc=np.random.rand(3),    # 3 元 RGB 序列,在 0~1 上均勻分布的 1 個 1*3 的數組
                ec=np.random.rand(3), lw=np.random.normal(loc=1, scale=1, size=1),    # 服從期望為 1, 標准差為 1 的正態分布 1 個 float
 ) ax.set(xlim=(-1, 11),    # 坐標軸刻度范圍 (xmin, xmax)
       ylim=(-1, 11),    # (ymin, ymax)
       fc='cornsilk', ) # 顯示圖形 
plt.show()

圖形:

 

 

軟件版本信息:

 

 


免責聲明!

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



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