基于 matplotlib module 的物理示意图绘制

本文详细介绍了如何使用matplotlib库在Python中创建物理示意图,包括矩形、圆形、椭圆、多边形、楔形以及带有弯曲箭头的FancyArrowPatch类实例。展示了如何设置各种图形元素的属性和连接样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 基于 matplotlib module 的物理示意图绘制
# 创建画布和子图
fig, ax = plt.subplots()

# 去除 x 轴和 y 轴的边框线
ax.spines['bottom'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)

# 隐藏 x 轴和 y 轴的刻度线和刻度标签
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])

# 设置坐标轴范围
ax.set_xlim([0, 15])
ax.set_ylim([0, 15])

matplotlib.patches 模块

Rectangle 类

import matplotlib.pyplot as plt
import matplotlib.patches as patches
rect = patches.Rectangle((0, 0), 2, 2, facecolor='red')

fig, ax = plt.subplots()
clip_rect = patches.Rectangle((0.5, 0.5), 0.1, 0.1)

rect.set_clip_path(clip_rect)

ax.add_patch(rect)

ax.set_xlim(-1, 3)
ax.set_ylim(-1, 3)

# 显示图形
plt.pause(0.01)


Circle 类

 

import matplotlib.pyplot as plt
import matplotlib.patches as patches
rect = patches.Rectangle((0, 0), 2, 2, facecolor='red')

fig, ax = plt.subplots()
circle = patches.Circle((0, 0), 1)
circle.set_edgecolor('red')
circle.set_facecolor('blue')
circle.set_linewidth(2)





ax.add_patch(circle)

ax.set_xlim(-1, 3)
ax.set_ylim(-1, 3)

# 显示图形
plt.pause(0.01)

 


Ellipse 类


Polygon 类


Wedge 类


 


FancyArrowPatch 类


# 绘制曲线
x = [1, 2, 3, 4, 5]
y = [2, 5, 4, 8, 1]
ax.plot(x, y)

# 添加弯曲箭头
start = (2, 4)  # 箭头起始点坐标
end = (5, 10)  # 箭头结束点坐标

# 使用FancyArrow类绘制弯曲箭头
arrow1 = FancyArrowPatch(start, end,
                            connectionstyle='arc3, rad=-0.4',  # 设置弯曲箭头的样式和曲率
                            mutation_scale=10,  # 控制箭头大小
                            color="red")

arrow2 = FancyArrowPatch(start, end,
                            connectionstyle='angle3, angleA=-60, angleB=-10  ',  # 设置弯曲箭头的样式和曲率
                            mutation_scale=10,  # 控制箭头大小
                            color="blue")


# 添加箭头到图中
ax.add_patch(arrow1)
ax.add_patch(arrow2)

# 显示图形
plt.pause(0.01)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值