Initial commit
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
import os
|
||||
|
||||
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")
|
||||
|
||||
import schemdraw
|
||||
import schemdraw.elements as elm
|
||||
from schemdraw.segments import SegmentText
|
||||
|
||||
FONT_NAME = "LXGW WenKai"
|
||||
OUTPUT_FILE = "terminal_voltage_diagram.svg"
|
||||
|
||||
|
||||
def add_text(
|
||||
drawing,
|
||||
position,
|
||||
text,
|
||||
*,
|
||||
rotation=0,
|
||||
align=("center", "center"),
|
||||
fontsize=18,
|
||||
):
|
||||
element = elm.Element().at(position).anchor("center").hold()
|
||||
element.anchors["center"] = (0, 0)
|
||||
element.segments.append(
|
||||
SegmentText(
|
||||
(0, 0),
|
||||
text,
|
||||
rotation=rotation,
|
||||
align=align,
|
||||
fontsize=fontsize,
|
||||
font=FONT_NAME,
|
||||
)
|
||||
)
|
||||
drawing.add(element)
|
||||
|
||||
|
||||
with schemdraw.Drawing(show=False) as d:
|
||||
d.config(
|
||||
unit=1.0,
|
||||
fontsize=18,
|
||||
font=FONT_NAME,
|
||||
lw=2.0,
|
||||
margin=0.18,
|
||||
bgcolor="white",
|
||||
)
|
||||
|
||||
# ===== 几何参数 =====
|
||||
left_x = 1.5
|
||||
top_y = 6.3
|
||||
bottom_y = 2.1
|
||||
|
||||
node_x = 6.1
|
||||
term_x = 8.45
|
||||
|
||||
# 电池位置
|
||||
battery_x = left_x
|
||||
battery_bottom_y = 3.55
|
||||
battery_len = 1.55
|
||||
|
||||
# 电阻尺寸控制
|
||||
top_res_start_x = 3.0
|
||||
top_res_len = 2.2
|
||||
|
||||
vertical_res_len = 2.25
|
||||
|
||||
# 关键点
|
||||
top_node = (node_x, top_y)
|
||||
bottom_node = (node_x, bottom_y)
|
||||
a_pos = (term_x, top_y)
|
||||
b_pos = (term_x, bottom_y)
|
||||
|
||||
top_battery_end_y = battery_bottom_y + battery_len
|
||||
|
||||
# ===== 左支路 + 电池 =====
|
||||
# 下导线到电池
|
||||
d.add(
|
||||
elm.Line()
|
||||
.at((battery_x, bottom_y))
|
||||
.up(battery_bottom_y - bottom_y)
|
||||
.hold()
|
||||
)
|
||||
|
||||
# 电池
|
||||
d.add(
|
||||
elm.BatteryCell()
|
||||
.at((battery_x, battery_bottom_y))
|
||||
.up(battery_len)
|
||||
.hold()
|
||||
)
|
||||
|
||||
# 电池上端到顶端导线
|
||||
d.add(
|
||||
elm.Line()
|
||||
.at((battery_x, top_battery_end_y))
|
||||
.up(top_y - top_battery_end_y)
|
||||
.hold()
|
||||
)
|
||||
|
||||
# ===== 上支路 =====
|
||||
d.add(elm.Line().at((battery_x, top_y)).right(top_res_start_x - battery_x).hold())
|
||||
d.add(elm.ResistorIEC().at((top_res_start_x, top_y)).right(top_res_len).hold())
|
||||
d.add(
|
||||
elm.Line()
|
||||
.at((top_res_start_x + top_res_len, top_y))
|
||||
.right(node_x - (top_res_start_x + top_res_len))
|
||||
.hold()
|
||||
)
|
||||
|
||||
# 上节点
|
||||
d.add(elm.Dot(radius=0.085).at(top_node).hold())
|
||||
|
||||
# a 端
|
||||
d.add(elm.Line().at(top_node).right(term_x - node_x).hold())
|
||||
d.add(elm.Dot(open=True, radius=0.105).at(a_pos).hold())
|
||||
|
||||
# ===== 中间竖支路 =====
|
||||
d.add(elm.ResistorIEC().at(top_node).down(vertical_res_len).hold())
|
||||
d.add(
|
||||
elm.Line()
|
||||
.at((node_x, top_y - vertical_res_len))
|
||||
.down((top_y - vertical_res_len) - bottom_y)
|
||||
.hold()
|
||||
)
|
||||
|
||||
# 下节点
|
||||
d.add(elm.Dot(radius=0.085).at(bottom_node).hold())
|
||||
|
||||
# b 端
|
||||
d.add(elm.Line().at(bottom_node).right(term_x - node_x).hold())
|
||||
d.add(elm.Dot(open=True, radius=0.105).at(b_pos).hold())
|
||||
|
||||
# ===== 下支路闭合 =====
|
||||
d.add(elm.Line().at((battery_x, bottom_y)).right(node_x - battery_x).hold())
|
||||
|
||||
# ===== 文本标注 =====
|
||||
# 9V:竖排,放在电池右侧偏中间,更贴近教材风格
|
||||
add_text(
|
||||
d,
|
||||
(battery_x + 0.92, battery_bottom_y + battery_len / 2),
|
||||
"10V",
|
||||
rotation=0,
|
||||
fontsize=16,
|
||||
)
|
||||
|
||||
# 1kΩ:水平,置于上方电阻正下方
|
||||
add_text(
|
||||
d,
|
||||
(top_res_start_x + top_res_len / 2, top_y - 0.72),
|
||||
"1kΩ",
|
||||
rotation=0,
|
||||
fontsize=16,
|
||||
)
|
||||
|
||||
# 2kΩ:竖排,置于竖直电阻左侧,方向要注意
|
||||
add_text(
|
||||
d,
|
||||
(node_x - 0.82, top_y - vertical_res_len / 2),
|
||||
"2kΩ",
|
||||
rotation=0,
|
||||
fontsize=16,
|
||||
)
|
||||
|
||||
# a / b 端标注
|
||||
add_text(
|
||||
d,
|
||||
(a_pos[0] + 0.42, a_pos[1] - 0.02),
|
||||
"a",
|
||||
fontsize=19,
|
||||
)
|
||||
add_text(
|
||||
d,
|
||||
(b_pos[0] + 0.42, b_pos[1] - 0.02),
|
||||
"b",
|
||||
fontsize=19,
|
||||
)
|
||||
|
||||
d.save(OUTPUT_FILE, transparent=False)
|
||||
@@ -0,0 +1,308 @@
|
||||
import os
|
||||
|
||||
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")
|
||||
|
||||
import schemdraw
|
||||
import schemdraw.elements as elm
|
||||
from schemdraw.segments import SegmentText, SegmentCircle
|
||||
|
||||
FONT_NAME = "LXGW WenKai"
|
||||
BG_COLOR = "white"
|
||||
|
||||
# 这里改成你自己的输出目录
|
||||
OUTDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "image"))
|
||||
os.makedirs(OUTDIR, exist_ok=True)
|
||||
|
||||
|
||||
def make_drawing():
|
||||
d = schemdraw.Drawing(show=False)
|
||||
d.config(
|
||||
unit=1.0,
|
||||
fontsize=16,
|
||||
font=FONT_NAME,
|
||||
lw=2.2,
|
||||
margin=0.18,
|
||||
bgcolor=BG_COLOR,
|
||||
)
|
||||
return d
|
||||
|
||||
|
||||
def add_text(
|
||||
drawing,
|
||||
position,
|
||||
content,
|
||||
*,
|
||||
rotation=0,
|
||||
align=("center", "center"),
|
||||
fontsize=16,
|
||||
):
|
||||
element = elm.Element().at(position).anchor("center").hold()
|
||||
element.anchors["center"] = (0, 0)
|
||||
element.segments.append(
|
||||
SegmentText(
|
||||
(0, 0),
|
||||
content,
|
||||
rotation=rotation,
|
||||
align=align,
|
||||
fontsize=fontsize,
|
||||
font=FONT_NAME,
|
||||
)
|
||||
)
|
||||
drawing.add(element)
|
||||
|
||||
|
||||
def add_dot(drawing, position, *, open_dot=False, radius=0.085):
|
||||
drawing.add(elm.Dot(open=open_dot, radius=radius).at(position).hold())
|
||||
|
||||
|
||||
def wire(drawing, start, end):
|
||||
drawing.add(elm.Line().at(start).to(end).hold())
|
||||
|
||||
|
||||
def save_drawing(drawing, filename):
|
||||
drawing.save(os.path.join(OUTDIR, filename), transparent=False)
|
||||
|
||||
|
||||
# -------------------------
|
||||
# 图 (a)
|
||||
# -------------------------
|
||||
def make_circuit_a():
|
||||
d = make_drawing()
|
||||
|
||||
x_left = 0.9
|
||||
x_meter = 3.0
|
||||
x_node = 5.7
|
||||
x_res_top = 7.2
|
||||
x_right = 10.1
|
||||
|
||||
y_top = 4.2
|
||||
y_bottom = 1.0
|
||||
y_mid = 2.55
|
||||
|
||||
# 左侧电池
|
||||
batt = d.add(elm.BatteryCell().at((x_left, y_bottom)).up(2.1).hold())
|
||||
wire(d, batt.end, (x_left, y_top))
|
||||
wire(d, batt.start, (x_left, y_bottom))
|
||||
|
||||
# 上边:A表 + 节点 + 1k
|
||||
wire(d, (x_left, y_top), (x_meter, y_top))
|
||||
am = d.add(elm.MeterA().at((x_meter, y_top)).right().hold())
|
||||
wire(d, am.end, (x_node, y_top))
|
||||
add_dot(d, (x_node, y_top))
|
||||
|
||||
wire(d, (x_node, y_top), (x_res_top, y_top))
|
||||
r1 = d.add(elm.ResistorIEC().at((x_res_top, y_top)).right(1.5).hold())
|
||||
wire(d, r1.end, (x_right, y_top))
|
||||
|
||||
# 右侧竖直 2k 电阻
|
||||
r2 = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
|
||||
wire(d, r2.end, (x_right, y_bottom))
|
||||
|
||||
# 中间 V 表支路
|
||||
wire(d, (x_node, y_top), (x_node, 3.45))
|
||||
vm = d.add(elm.MeterV().at((x_node, 3.45)).down().hold())
|
||||
wire(d, vm.end, (x_node, y_bottom))
|
||||
add_dot(d, (x_node, y_bottom))
|
||||
|
||||
# 底边闭合
|
||||
wire(d, (x_left, y_bottom), (x_node, y_bottom))
|
||||
wire(d, (x_node, y_bottom), (x_right, y_bottom))
|
||||
|
||||
# 文字
|
||||
add_text(d, (8.0, 4.75), "1kΩ", fontsize=16)
|
||||
add_text(d, (10.75, 2.45), "2kΩ", fontsize=16)
|
||||
add_text(d, (5.4, 0.25), "(a)", fontsize=18)
|
||||
|
||||
save_drawing(d, "circuit_a.svg")
|
||||
|
||||
|
||||
# -------------------------
|
||||
# 图 (b)
|
||||
# -------------------------
|
||||
def make_circuit_b():
|
||||
d = make_drawing()
|
||||
|
||||
x_left = 0.9
|
||||
x_node = 3.4
|
||||
x_meter = 5.0
|
||||
x_res_top = 7.2
|
||||
x_right = 10.1
|
||||
|
||||
y_top = 4.2
|
||||
y_bottom = 1.0
|
||||
|
||||
# 左侧电池
|
||||
batt = d.add(elm.BatteryCell().at((x_left, y_bottom)).up(2.1).hold())
|
||||
wire(d, batt.end, (x_left, y_top))
|
||||
wire(d, batt.start, (x_left, y_bottom))
|
||||
|
||||
# 上边左节点
|
||||
wire(d, (x_left, y_top), (x_node, y_top))
|
||||
add_dot(d, (x_node, y_top))
|
||||
|
||||
# 中间 V 表
|
||||
wire(d, (x_node, y_top), (x_node, 3.45))
|
||||
vm = d.add(elm.MeterV().at((x_node, 3.45)).down().hold())
|
||||
wire(d, vm.end, (x_node, y_bottom))
|
||||
add_dot(d, (x_node, y_bottom))
|
||||
|
||||
# 上边右支路:A + 1k
|
||||
wire(d, (x_node, y_top), (x_meter, y_top))
|
||||
am = d.add(elm.MeterA().at((x_meter, y_top)).right().hold())
|
||||
wire(d, am.end, (x_res_top, y_top))
|
||||
r1 = d.add(elm.ResistorIEC().at((x_res_top, y_top)).right(1.5).hold())
|
||||
wire(d, r1.end, (x_right, y_top))
|
||||
|
||||
# 右侧竖直 2k
|
||||
r2 = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
|
||||
wire(d, r2.end, (x_right, y_bottom))
|
||||
|
||||
# 下边闭合
|
||||
wire(d, (x_left, y_bottom), (x_node, y_bottom))
|
||||
wire(d, (x_node, y_bottom), (x_right, y_bottom))
|
||||
|
||||
# 文字
|
||||
add_text(d, (8.0, 4.75), "1kΩ", fontsize=16)
|
||||
add_text(d, (9.1, 3.0), "2kΩ", fontsize=16)
|
||||
add_text(d, (5.6, 0.25), "(b)", fontsize=18)
|
||||
|
||||
save_drawing(d, "circuit_b.svg")
|
||||
|
||||
|
||||
# -------------------------
|
||||
# 图 (c)
|
||||
# -------------------------
|
||||
def make_circuit_c():
|
||||
d = make_drawing()
|
||||
|
||||
x_left = 1.2
|
||||
x_v = 3.2
|
||||
x_node = 6.0
|
||||
x_right = 8.8
|
||||
|
||||
y_top = 4.2
|
||||
y_bottom = 1.0
|
||||
|
||||
# 左侧电压源
|
||||
# 左侧理想电压源:圆圈 + 竖线
|
||||
source_center_y = 2.45
|
||||
source_radius = 0.55
|
||||
|
||||
# 上下导线
|
||||
wire(d, (x_left, y_top), (x_left, source_center_y + source_radius))
|
||||
wire(d, (x_left, source_center_y - source_radius), (x_left, y_bottom))
|
||||
|
||||
# 圆圈
|
||||
src = elm.Element().at((x_left, source_center_y)).anchor("center").hold()
|
||||
src.anchors["center"] = (0, 0)
|
||||
src.segments.append(SegmentCircle((0, 0), radius=source_radius))
|
||||
d.add(src)
|
||||
|
||||
# 圆圈中间竖线
|
||||
wire(d, (x_left, source_center_y - 0.5), (x_left, source_center_y + 0.5))
|
||||
|
||||
# 顶部 V 表
|
||||
wire(d, (x_left, y_top), (x_v, y_top))
|
||||
vm = d.add(elm.MeterV().at((x_v, y_top)).right().hold())
|
||||
wire(d, vm.end, (x_node, y_top))
|
||||
add_dot(d, (x_node, y_top))
|
||||
|
||||
# 右侧电阻支路
|
||||
wire(d, (x_node, y_top), (x_right, y_top))
|
||||
r = d.add(elm.ResistorIEC().at((x_right, y_top)).down(1.9).hold())
|
||||
wire(d, r.end, (x_right, y_bottom))
|
||||
|
||||
# 中间开关支路
|
||||
wire(d, (x_node, y_top), (x_node, 3.2))
|
||||
add_dot(d, (x_node, 3.2), open_dot=True, radius=0.11)
|
||||
add_dot(d, (x_node, 2.1), open_dot=True, radius=0.11)
|
||||
d.add(elm.Line().at((x_node - 0.05, 2.18)).to((x_node + 0.35, 2.95)).hold())
|
||||
wire(d, (x_node, 2.1), (x_node, y_bottom))
|
||||
add_dot(d, (x_node, y_bottom))
|
||||
|
||||
# 下边闭合
|
||||
wire(d, (x_left, y_bottom), (x_node, y_bottom))
|
||||
wire(d, (x_node, y_bottom), (x_right, y_bottom))
|
||||
|
||||
# 文字
|
||||
add_text(d, (0.25, 2.25), "Uₛ", fontsize=18)
|
||||
add_text(d, (6.65, 2.6), "K", fontsize=18)
|
||||
add_text(d, (9.5, 2.55), "R", fontsize=18)
|
||||
|
||||
save_drawing(d, "circuit_c.svg")
|
||||
|
||||
|
||||
# -------------------------
|
||||
# 图 (d)
|
||||
# -------------------------
|
||||
def make_circuit_d():
|
||||
d = make_drawing()
|
||||
|
||||
x_left = 1.0
|
||||
x_mid = 4.2
|
||||
x_right = 7.2
|
||||
|
||||
y_top = 6.5
|
||||
y_bottom = 0.7
|
||||
|
||||
# 外框
|
||||
wire(d, (x_left, y_top), (x_mid, y_top))
|
||||
add_dot(d, (x_mid, y_top))
|
||||
wire(d, (x_mid, y_top), (x_right, y_top))
|
||||
|
||||
wire(d, (x_left, y_bottom), (x_mid, y_bottom))
|
||||
add_dot(d, (x_mid, y_bottom))
|
||||
wire(d, (x_mid, y_bottom), (x_right, y_bottom))
|
||||
|
||||
# 左支路:电流源
|
||||
# 左支路:理想电流源(圆圈 + 竖线)
|
||||
source_center_y = 4.2
|
||||
source_radius = 0.55
|
||||
|
||||
wire(d, (x_left, y_top), (x_left, source_center_y + source_radius))
|
||||
wire(d, (x_left, source_center_y - source_radius), (x_left, y_bottom))
|
||||
|
||||
# 圆圈
|
||||
src = elm.Element().at((x_left, source_center_y)).anchor("center").hold()
|
||||
src.anchors["center"] = (0, 0)
|
||||
src.segments.append(SegmentCircle((0, 0), radius=source_radius))
|
||||
d.add(src)
|
||||
|
||||
# 圆圈中间竖线
|
||||
wire(d, (x_left, source_center_y - 0.5), (x_left, source_center_y + 0.5))
|
||||
|
||||
# 标注
|
||||
add_text(d, (x_left + 0.7, source_center_y - 0.6), "Iₛ", fontsize=18)
|
||||
|
||||
# 右支路:A 表
|
||||
wire(d, (x_right, y_top), (x_right, 5.0))
|
||||
am = d.add(elm.MeterA().at((x_right, 5.0)).down().hold())
|
||||
wire(d, am.end, (x_right, y_bottom))
|
||||
|
||||
# 中间支路:开关 + 可变电阻
|
||||
wire(d, (x_mid, y_top), (x_mid, 5.2))
|
||||
add_dot(d, (x_mid, 5.2), open_dot=True, radius=0.11)
|
||||
add_dot(d, (x_mid, 4.25), open_dot=True, radius=0.11)
|
||||
d.add(elm.Line().at((x_mid - 0.05, 4.35)).to((x_mid + 0.4, 5.0)).hold())
|
||||
wire(d, (x_mid, 4.25), (x_mid, 3.6))
|
||||
|
||||
r = d.add(elm.ResistorIEC().at((x_mid, 3.6)).down(1.5).hold())
|
||||
wire(d, r.end, (x_mid, y_bottom))
|
||||
|
||||
# 文字
|
||||
add_text(d, (4.85, 4.85), "K", fontsize=18)
|
||||
add_text(d, (4.8, 2.05), "R", fontsize=18)
|
||||
|
||||
save_drawing(d, "circuit_d.svg")
|
||||
|
||||
|
||||
def main():
|
||||
make_circuit_a()
|
||||
make_circuit_b()
|
||||
make_circuit_c()
|
||||
make_circuit_d()
|
||||
print("Created: circuit_a.svg, circuit_b.svg, circuit_c.svg, circuit_d.svg")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user