使用schemdraw-markdown库绘制电路图
在markdown
中绘制电路图并不容易,人工智能会建议使用mermaid
、PlantUML
等工具,实际上这俩绘制UML
图表很轻松,但画电路图就有点捉襟见肘了。
然后找到一个叫做schemdraw-markdown
的库,它可以通过Python
代码的形式画电路图,相当特别,那么就开始我们的实验吧!
安装schemdraw-markdown
$ pip install schemdraw-markdown
包引入
可以通过一下代码引用包:
import schemdraw
import schemdraw.elements as elm
电路绘制
电路绘制代码如下:
with schemdraw.Drawing():elm.Resistor().right().label('1Ω')elm.Capacitor().down().label('10μF')elm.Line().left()elm.SourceSin().up().label('10V')
也可以画一些相对复杂点的电路,如电源供电电路:
with schemdraw.Drawing() as d:d.config(inches_per_unit=.5, unit=3)D = elm.Rectifier()elm.Line().left(d.unit*1.5).at(D.N).dot(open=True).idot()elm.Line().left(d.unit*1.5).at(D.S).dot(open=True).idot()G = elm.Gap().toy(D.N).label(['–', 'AC IN', '+'])top = elm.Line().right(d.unit*3).at(D.E).idot()Q2 = elm.BjtNpn(circle=True).up().anchor('collector').label('Q2\n2n3055')elm.Line().down(d.unit/2).at(Q2.base)Q2b = elm.Dot()elm.Line().left(d.unit/3)Q1 = elm.BjtNpn(circle=True).up().anchor('emitter').label('Q1\n 2n3054')elm.Line().at(Q1.collector).toy(top.center).dot()elm.Line().down(d.unit/2).at(Q1.base).dot()elm.Zener().down().reverse().label('D2\n500mA', loc='bot').dot()G = elm.Ground()elm.Line().left().dot()elm.Capacitor(polar=True).up().reverse().label('C2\n100$\\mu$F\n50V', loc='bot').dot()elm.Line().right().hold()elm.Resistor().toy(top.end).label('R1\n2.2K\n50V', loc='bot').dot()d.move(dx=-d.unit, dy=0)elm.Capacitor(polar=True).toy(G.start).flip().label('C1\n 1000$\\mu$F\n50V').dot().idot()elm.Line().at(G.start).tox(D.W)elm.Line().toy(D.W).dot()elm.Resistor().right().at(Q2b.center).label('R2').label('56$\\Omega$ 1W', loc='bot').dot()d.push()elm.Line().toy(top.start).dot()elm.Line().tox(Q2.emitter)d.pop()elm.Capacitor(polar=True).toy(G.start).label('C3\n470$\\mu$F\n50V', loc='bot').dot()elm.Line().tox(G.start).hold()elm.Line().right().dot()elm.Resistor().toy(top.center).label('R3\n10K\n1W', loc='bot').dot()elm.Line().left().hold()elm.Line().right()elm.Dot(open=True)elm.Gap().toy(G.start).label(['+', '$V_{out}$', '–'])elm.Dot(open=True)elm.Line().left()
或常见的RCL电路:
# 创建绘图对象
with schemdraw.Drawing() as d:v_in_up = elm.Dot(open=True)L = elm.Inductor().label('$L$')R = elm.Resistor().label('$R$')d.push()elm.Line().length(1).right()v_out_up = elm.Dot(open=True)d.pop()C = elm.Capacitor().down().label('$C$')d.push()line = elm.Line().length(6).left()v_in_down = elm.Dot(open=True)elm.Gap().up().label('$v_{in}$')d.pop()elm.Line().length(1).right()v_out_down = elm.Dot(open=True)elm.Gap().up().label('$v_{out}$')elm.LoopCurrent([R, C, line, v_in_down]).label('$i$')