Search code examples
pythonmatplotlibjupyter-notebookdrawingcircuit-diagram

How to draw a schematic using SchemDraw without using Jupyter Notebook?


The code example below runs without error, but nothing is displayed to the screen.

Here's the code example that I'm trying to use...

import SchemDraw
import SchemDraw.elements as elm

d = SchemDraw.Drawing()
R1 = d.add(elm.Resistor(label='1K$\Omega$'))
d.labelI(R1, '1 mA', top=False)
d.add(elm.Capacitor(d='down', botlabel='0.1$\mu$F'))
d.add(elm.Line( d='Left'))
d.add(elm.Ground)
d.add(elm.SourceV( d='up', label='10V') )
d.save('schematic.svg')
d.draw()

I'm on a Windows 7 platform and I have Python 3.7 integrated into my command prompt. If I navigate to the directory where my schematic.py file is located and I add this into the console:

Python schematic.py

It runs fine and exits with 0 error, but nothing is drawn to the screen, Matplotlib isn't even invoked...

After searching through some documents, brief tutorials, or examples that are very limited, I've come to realize that the above example, as well as others, rely on Jupyter Notebook with Matplotlib inlined...


How am I able to draw this without using Jupyter Notebook and inlining Matplotlib directly?

I would like to run it as a basic Python script and I know I can import the Matplotlib module manually like this...

import Matplotlib.pyplot as plt

//... prev code
d.draw() // doesn't draw anything to the screen or to matplotlib's backend...
// plt.plot(...)? What goes here?
plt.show()

But I don't know how to use it to draw the results from SchemDraw's draw method...


Solution

  • Losing the Matplotlib interactive window was a regression bug introduced in SchemDraw 0.7. It was fixed in 0.7.1, pushed to PyPi today. In this version, d.draw() opens the Matplotlib window if running as a script, or shows output in the cell if running in Jupyter inline mode.