Drawing

class schemdraw.Drawing(canvas=None, file=None, show=True, transparent=False, dpi=72, **kwargs)

A schematic drawing

See schemdraw.config method for argument defaults

Parameters:
  • canvas (Optional[Union[Backends, xml.etree.ElementTree.Element, matplotlib.pyplot.Axes]]) – Canvas to draw on when using Drawing context manager. Can be string ‘matplotlib’ or ‘svg’ to create new canvas with these backends, or an instance of a matplotlib axis, or an instance of xml.etree.ElementTree containing SVG. Default is value set by schemdraw.use().

  • file (Optional[str]) – optional filename to save on exiting context manager or calling draw method.

  • show (bool) – Show the drawing after exiting context manager

  • transparent (bool) – Save to file with a transparent background

  • dpi (float) – Dots-per-inch when saving to a raster graphics file

here

(xy tuple) Current drawing position. The next element will be added at this position unless specified otherwise.

theta

(float) Current drawing angle, in degrees. The next element will be added with this angle unless specified otherwise.

class HoldState(dwg)

Context manager to save the drawing state

add(element)

Add an element to the drawing.

Parameters:

element (Element) – The element to add.

Return type:

Element

add_elements(*elements)

Add multiple elements to the drawing

Return type:

None

add_svgdef(svgdef)

Add an item to the SVG <defs> element

Return type:

None

config(unit=None, inches_per_unit=None, fontsize=None, font=None, color=None, lw=None, ls=None, fill=None, bgcolor=None, margin=None, mathfont=None)

Set Drawing configuration, overriding schemdraw global config.

Parameters:
  • unit (Optional[float]) – Full length of a 2-terminal element. Inner zig-zag portion of a resistor is 1.0 units.

  • inches_per_unit (Optional[float]) – Inches per drawing unit for setting drawing scale

  • fontsize (Optional[float]) – Default font size for text labels

  • font (Optional[str]) – Default font family for text labels

  • mathfont (Optional[str]) – Default font family or filename math text delimited by $..$

  • color (Optional[str]) – Default color name or RGB (0-1) tuple

  • lw (Optional[float]) – Default line width for elements

  • ls (Optional[Literal['-', ':', '--', '-.']]) – Default line style

  • fill (Optional[str]) – Deault fill color for closed elements

  • margin (Optional[float]) – White space around the drawing in drawing units

Return type:

None

container(cornerradius=0.3, padx=0.75, pady=0.75)

Add a container to the drawing. Use as a context manager, such that elemnents inside the with are surrounded by the container.

>>> with drawing.container():
>>>    elm.Resistor()
>>>    ...
Parameters:
  • cornerradius (float) – radius for box corners

  • padx (float) – space between contents and border in x direction

  • pady (float) – space between contents and border in y direction

draw(show=True, canvas=None)

Draw the schematic

Parameters:
  • show (bool) – Show the schematic in a GUI popup window (when outside of a Jupyter inline environment)

  • canvas – ‘matplotlib’, ‘svg’, or Axis instance to draw on

Returns:

schemdraw Figure object

get_bbox()

Get drawing bounding box

Return type:

BBox

get_imagedata(fmt='svg')

Get image data as bytes array

Parameters:

fmt (ImageFormat | ImageType) – Format or file extension of the image type. SVG backend only supports ‘svg’ format.

Return type:

bytes

Returns:

Image data as bytes

get_segments()

Get flattened list of all segments in the drawing

Return type:

list[Union[Segment, SegmentText, SegmentPoly, SegmentArc, SegmentCircle, SegmentBezier, SegmentPath, SegmentImage]]

hold()

Return context manager to hold the current drawing state and restore it when the with-block ends

interactive(interactive=True)

Enable interactive mode (matplotlib backend only). Matplotlib must also be set to interactive with plt.ion().

move(dx=0, dy=0)

Move the current drawing position

Parameters:
  • dx (float) – change in x position

  • dy (float) – change in y position

Return type:

None

move_from(ref, dx=0, dy=0, theta=None)

Move drawing position relative to the reference point. Change drawing theta if provided.

Return type:

None

pop()

Pop/load the drawing state. Location and angle are returned to previously pushed state.

Return type:

None

push()

Push/save the drawing state. Drawing.here and Drawing.theta are saved.

Return type:

None

save(fname, transparent=True, dpi=72)

Save figure to a file

Parameters:
  • fname (str) – Filename to save. In Matplotlib backend, the file type is automatically determined from extension (png, svg, jpg). SVG backend only supports saving SVG format.

  • transparent (bool) – Save as transparent background, if available

  • dpi (float) – Dots-per-inch for raster formats

Return type:

None

set_anchor(name)

Define a Drawing anchor at the current drawing position

Return type:

None

undo()

Removes previously added element

Return type:

None

Element

class schemdraw.elements.Element(**kwargs)

Standard circuit element.

Keyword Arguments are equivalent to calling setter methods.

anchors

Dictionary of anchor positions in element coordinates

absanchors

Dictionary of anchor positions in absolute drawing coordinates

segments

List of drawing primitives making up the element

transform

Transformation from element to drawing coordinates

absdrop

Drop position in drawing coordinates, set after the element is added to a drawing

defaults

Default parameters for the element

Anchor names are dynmically added as attributes after placing the element in a Drawing.

anchor(anchor)

Specify anchor for placement. The anchor will be aligned with the position specified by at() method.

Return type:

Element

at(xy, dx=0, dy=0)

Set the element xy position

Parameters:

xy (XY | tuple[‘Element’, str]) – (x,y) position or tuple of (Element, anchorname)

Return type:

Element

color(color)

Sets the element color

Parameters:

color (str) – color name or hex value (ie ‘#FFFFFF’)

Return type:

Element

down()

Set the direction to down

Return type:

Element

drop(drop)

Set the drop position - where to leave the current drawing position after placing this element

Return type:

Element

fill(color=True)

Sets the element fill color.

Parameters:
  • color (bool | str) – Color string name or hex value, or

  • color. (True to fill with the element line)

Return type:

Element

flip()

Apply flip up/down

Return type:

Element

get_bbox(transform=False, includetext=True)

Get element bounding box

Parameters:
  • transform – Apply the element transform to the bbox to get bounds in Drawing coordinates

  • includetext – Consider text when calculating bounding box. Text width and height can vary by font, so this produces an estimate of bounds.

Returns:

Corners of the bounding box, (xmin, ymin, xmax, ymax)

gradient_fill(color1, color2, vertical=True)

Fill the element with a linear gradient between two colors. Only supported in SVG backend.

Return type:

Element

hold()

Do not move the Drawing here position after placing this element

Return type:

Element

label(label, loc=None, ofst=None, halign=None, valign=None, rotate=False, fontsize=None, font=None, mathfont=None, color=None, href=None, decoration=None)

Add a label to the Element.

Parameters:
  • label (str | Sequence[str]) – The text string or list of strings. If list, each string will be evenly spaced along the element (e.g. [‘-’, ‘V’, ‘+’])

  • loc (Optional[LabelLoc]) – Label position within the Element. Either (‘top’, ‘bottom’, ‘left’, ‘right’), or the name of an anchor within the Element.

  • ofst (XY | float | None) – Offset from default label position

  • halign (Optional[Halign]) – Horizontal text alignment (‘center’, ‘left’, ‘right’)

  • valign (Optional[Valign]) – Vertical text alignment (‘center’, ‘top’, ‘bottom’)

  • rotate (bool | float) – True to rotate label with element, or specify rotation angle in degrees

  • fontsize (Optional[float]) – Size of label font

  • font (Optional[str]) – Name/font-family of label text

  • mathfont (Optional[str]) – Name/font-family of math text

  • color (Optional[str]) – Color of label

  • href (Optional[str]) – Hyperline target (jump to)

  • decoration (Optional[str]) – “underline” or “overline”

left()

Set the direction to left

Return type:

Element

linestyle(ls)

Sets the element line style

Parameters:

ls (Literal['-', ':', '--', '-.']) – Line style (‘-’, ‘:’, ‘–’, ‘-.’).

Return type:

Element

linewidth(lw)

Sets the element line width

Parameters:

lw (float) – Line width

Return type:

Element

reverse()

Apply reverse left/right

Return type:

Element

right()

Set the direction to right

Return type:

Element

scale(scale=1)

Apply scale/zoom factor to element

Return type:

Element

scalex(scale=1)

Apply horizontal scale/zoom to element

Return type:

Element

scaley(scale=1)

Apply vertical scale/zoom to element

Return type:

Element

style(color=None, fill=None, ls=None, lw=None)

Apply all style parameters

Parameters:
  • color (Optional[str]) – Color string or hex value

  • fill (Optional[str]) – Color string or hex

  • ls (Optional[Literal['-', ':', '--', '-.']]) – Line style (‘-’, ‘:’, ‘–’, ‘-.’)

  • lw (Optional[float]) – Line width

Return type:

Element

theta(theta)

Set the drawing direction angle in degrees

Return type:

Element

up()

Set the direction to up

Return type:

Element

zorder(zorder)

Sets the element zorder. Higher zorders will be drawn above lower zorder elements.

Return type:

Element

Element2Term

class schemdraw.elements.Element2Term(**kwargs)

Two terminal element. The element leads can be automatically extended to the start and ending positions.

Anchors:
  • start

  • center

  • end

dot(open=False)

Add a dot to the end of the element

Return type:

Element2Term

down(length=None)

Set the direction to down

Return type:

Element

endpoints(start, end)

Sets absolute endpoints of element

Return type:

Element2Term

idot(open=False)

Add a dot to the input/start of the element

Return type:

Element2Term

left(length=None)

Set the direction to left

Return type:

Element

length(length)

Sets total length of element

Return type:

Element2Term

right(length=None)

Set the direction to right

Return type:

Element

shift(shift)

Shift the element within its leads to one end or the other. The shift parameter should be between -1 and 1, indicating fraction of the lead extension.

Return type:

Element2Term

to(xy, dx=0, dy=0)

Sets ending position of element

Parameters:
  • xy (Union[Tuple[float, float], Point]) – Ending position of element

  • dx (float) – X-offset from xy position

  • dy (float) – Y-offset from xy position

Return type:

Element2Term

tox(x)

Sets ending x-position of element (for horizontal elements)

Return type:

Element2Term

toy(y)

Sets ending y-position of element (for vertical elements)

Return type:

Element2Term

up(length=None)

Set the direction to up

Return type:

Element

ElementDrawing

class schemdraw.elements.ElementDrawing(*args, **kwargs)

Create an element from a Drawing

Parameters:

drawing – The Drawing instance to convert to an element

ElementImage

class schemdraw.elements.ElementImage(image, width, height, xy=(0, 0), imgfmt=None, **kwargs)

Element from an Image file

Parameters:
  • image (str | BinaryIO) – Image filename or open file pointer

  • width (float) – Width to draw image in Drawing

  • height (float) – Height to draw image in Drawing

  • xy (Point) – Origin (lower left corner)

Element Style

schemdraw.elements.style(style)

Set global element style

Parameters:

style – dictionary of {elementname: Element} to change the element module namespace. Use elements.STYLE_US or elements.STYLE_IEC to define U.S. or European/IEC element styles.

schemdraw.config(unit=3.0, inches_per_unit=0.5, lblofst=0.1, fontsize=14.0, font='sans-serif', color='black', lw=2.0, ls='-', fill=None, bgcolor=None, margin=0.1, mathfont=None)

Set global schemdraw style configuration

Parameters:
  • unit (float) – Full length of a 2-terminal element. Inner zig-zag portion of a resistor is 1.0 units.

  • inches_per_unit (float) – Inches per drawing unit for setting drawing scale

  • lblofst (float) – Default offset between element and its label

  • fontsize (float) – Default font size for text labels

  • font (str) – Default font family for text labels

  • color (str) – Default color name or RGB (0-1) tuple

  • lw (float) – Default line width for elements

  • ls (Literal['-', ':', '--', '-.']) – Default line style

  • fill (Optional[str]) – Deault fill color for closed elements

  • margin (float) – White space around the drawing in drawing units

  • mathont – Font for math delimited by $..$

Return type:

None

schemdraw.theme(theme='default')

Set schemdraw theme (line color and background color). Themes match those in jupyter-themes package (https://github.com/dunovank/jupyter-themes).

Available themes:
  • default (black on white)

  • dark (white on black)

  • solarizedd

  • solarizedl

  • onedork

  • oceans16

  • monokai

  • gruvboxl

  • gruvboxd

  • grade3

  • chesterish

schemdraw.use(backend='matplotlib')

Change default backend, either ‘matplotlib’ or ‘svg’

Return type:

None