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()
图形:
软件版本信息:
非学无以广才,非志无以成学。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架