I’m the kind of developer who learns best when I see something appear on-screen. So when I found out that Schemdraw can turn a few lines of Python into a polished schematic, I had to try it. Below is the full journey: the bare-bones code, the inevitable error I hit, the upgrades I bolted on, and a handful of practice ideas you can steal
My Code
schemdraw
import schemdraw.elements as elm
d = schemdraw.Drawing()
V = d.add(elm.SourceV().label('V', loc='left')) # Voltage source
R = d.add(elm.Resistor().label('R').right()) # Resistor
C = d.add(elm.Capacitor().label('C').right()) # Capacitor
L = d.add(elm.Inductor().label('L').right()) # Inductor
d.add(elm.Ground().at(V.start)) # Ground at the negative terminal
d.add(elm.Ground().at(L.end)) # Ground at the far end
d.draw()
Run that after installing Schemdraw (pip install schemdraw
) and you’ll see a neat series R-C-L network with the source on the left and ground rails at both ends.
Explain Code
Line | What I typed | What it actually does |
---|---|---|
import schemdraw | Pulls in the library | Gives me the Drawing class and plotting backend |
import schemdraw.elements as elm | Shortcut alias | Lets me reference parts as elm.Resistor() instead of the long path |
d = schemdraw.Drawing() | Makes a blank canvas | All later parts attach to this object |
V = d.add(elm.SourceV()...) | Adds a voltage source | I label it V and pin the label to the left |
R = d.add(elm.Resistor().right()) | Adds a resistor to the right | .right() makes the next part start where the last one ended |
C = d.add(elm.Capacitor()...) | Adds the capacitor | Same chaining trick |
L = d.add(elm.Inductor()...) | Adds the inductor | Finishes the series string |
d.add(elm.Ground().at(V.start)) | Drops ground at the source’s minus side | Keeps the drawing tidy and realistic |
d.draw() | Renders the figure | Opens a Matplotlib window or saves if I choose |
The First Error
ModuleNotFoundError: No module named 'schemdraw'
I forgot to install the package on this machine. A quick:
pip install schemdraw
sorted it out.
Other hiccups you might see:
Error text | Why it shows up | Quick cure |
---|---|---|
AttributeError: 'SourceV' object has no attribute 'start' | You’re on an ancient Schemdraw (< 0.15) | pip install --upgrade schemdraw |
“Blank figure” on a headless server | No GUI backend for Matplotlib | Add: |
import matplotlib
matplotlib.use('Agg')
and call d.save('circuit.png')
instead of d.draw()
|
Correct Code
I wanted labels, automatic PNG export, and a quick “netlist” print-out so students can map nodes. Here’s the result:
import schemdraw
import schemdraw.elements as elm
import matplotlib
matplotlib.use('Agg') # comment out for an interactive window
def draw_series_rlc(v_label='V',
r_val='10 Ω',
c_val='1 µF',
l_val='100 mH',
fname='series_rlc.png'):
"""
Draw a labelled series RLC circuit and save it.
"""
d = schemdraw.Drawing()
V = d.add(elm.SourceV().label(v_label, loc='left'))
R = d.add(elm.Resistor().label(r_val).right())
C = d.add(elm.Capacitor().label(c_val).right())
L = d.add(elm.Inductor().label(l_val).right())
d.add(elm.Ground().at(V.start))
d.add(elm.Ground().at(L.end))
d.save(fname)
print(f'Diagram saved to {fname}')
# mini-netlist for spice fans
print('\nNetlist:')
print('N1 V 0')
print(f'N1 R N2 {r_val}')
print(f'N2 C N3 {c_val}')
print(f'N3 L 0 {l_val}')
if __name__ == '__main__':
draw_series_rlc() # uses the defaults
Define Change
- Parameters let me swap part values without hacking the code.
- The picture drops to series_rlc.png handy for slides or lab sheets.
- The quick netlist means I can paste straight into SPICE and simulate.
Explain Code
- Tweak the values
draw_series_rlc(r_val='47 Ω', c_val='4.7 nF', l_val='220 µH')
- Re-order parts
Move the capacitor first and watch how the diagram updates. - Add a parallel branch
d.push(); d.add(elm.Capacitor().down()); d.pop()
- Export to SPICE
Pipe the printed netlist into LTspice or ngspice, then compare the step response. - Color-code elements
Passcolor='blue'
into any element call to highlight it for teaching.
Final Thought
Drawing circuits in code felt gimmicky until I tried it. Now I reach for Schemdraw every time I need a quick schematic for notes, slides, or blog posts like this. The library is tiny, the API is readable, and the payoff a clean vector graphic in seconds beats hand-fiddling in a GUI.