Site icon FSIBLOG

How to Make an RLC Circuit in Python

How to Make an RLC Circuit in Python

How to Make an RLC Circuit in Python

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

LineWhat I typedWhat it actually does
import schemdrawPulls in the libraryGives me the Drawing class and plotting backend
import schemdraw.elements as elmShortcut aliasLets me reference parts as elm.Resistor() instead of the long path
d = schemdraw.Drawing()Makes a blank canvasAll later parts attach to this object
V = d.add(elm.SourceV()...)Adds a voltage sourceI 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 capacitorSame chaining trick
L = d.add(elm.Inductor()...)Adds the inductorFinishes the series string
d.add(elm.Ground().at(V.start))Drops ground at the source’s minus sideKeeps the drawing tidy and realistic
d.draw()Renders the figureOpens 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 textWhy it shows upQuick 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 serverNo GUI backend for MatplotlibAdd:
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

Explain Code

draw_series_rlc(r_val='47 Ω', c_val='4.7 nF', l_val='220 µH')
d.push(); d.add(elm.Capacitor().down()); d.pop()

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.

Exit mobile version