Drawing¶
- class schemdraw.Drawing(canvas: Backends | xml.etree.ElementTree.Element | matplotlib.pyplot.Axes | None = None, file: str | None = None, show: bool = True, **kwargs)¶
A schematic drawing
See schemdraw.config method for argument defaults
- Parameters:
canvas – 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 filename to save on exiting context manager or calling draw method.
show – Show the drawing after exiting context manager
- 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.
- add(element: Element) Element ¶
Add an element to the drawing.
- Parameters:
element – The element to add.
- config(unit: float | None = None, inches_per_unit: float | None = None, fontsize: float | None = None, font: str | None = None, color: str | None = None, lw: float | None = None, ls: Literal['-', ':', '--', '-.'] | None = None, fill: str | None = None, bgcolor: str | None = None, margin: float | None = None, mathfont: str | None = None) None ¶
Set Drawing configuration, overriding schemdraw global config.
- Parameters:
unit – Full length of a 2-terminal element. Inner zig-zag portion of a resistor is 1.0 units.
inches_per_unit – Inches per drawing unit for setting drawing scale
fontsize – Default font size for text labels
font – Default font family for text labels
mathfont – Default font family or filename math text delimited by $..$
color – Default color name or RGB (0-1) tuple
lw – Default line width for elements
ls – Default line style
fill – Deault fill color for closed elements
margin – White space around the drawing in drawing units
- container(cornerradius: float = 0.3, padx: float = 0.75, pady: float = 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 – radius for box corners
padx – space between contents and border in x direction
pady – space between contents and border in y direction
- draw(show: bool = True, canvas=None, backend: Literal['svg', 'matplotlib'] | None = None)¶
Draw the schematic
- Parameters:
show – Show the schematic in a GUI popup window (when outside of a Jupyter inline environment)
canvas – ‘matplotlib’, ‘svg’, or Axis instance to draw on
backend (deprecated) – ‘matplotlib’ or ‘svg’
- Returns:
schemdraw Figure object
- get_bbox() BBox ¶
Get drawing bounding box
- get_imagedata(fmt: ImageFormat | ImageType = 'svg') bytes ¶
Get image data as bytes array
- Parameters:
fmt – Format or file extension of the image type. SVG backend only supports ‘svg’ format.
- Returns:
Image data as bytes
- get_segments() list[SegmentType] ¶
Get flattened list of all segments in the drawing
- interactive(interactive: bool = True)¶
Enable interactive mode (matplotlib backend only). Matplotlib must also be set to interactive with plt.ion().
- move(dx: float = 0, dy: float = 0) None ¶
Move the current drawing position
- Parameters:
dx – change in x position
dy – change in y position
- move_from(ref: Point, dx: float = 0, dy: float = 0, theta: float | None = None) None ¶
Move drawing position relative to the reference point. Change drawing theta if provided.
- pop() None ¶
Pop/load the drawing state. Location and angle are returned to previously pushed state.
- push() None ¶
Push/save the drawing state. Drawing.here and Drawing.theta are saved.
- save(fname: str, transparent: bool = True, dpi: float = 72) None ¶
Save figure to a file
- Parameters:
fname – 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 – Save as transparent background, if available
dpi – Dots-per-inch for raster formats
- set_anchor(name: str) None ¶
Define a Drawing anchor at the current drawing position
- undo() None ¶
Removes previously added element
Element¶
- class schemdraw.elements.Element(*args, **kwargs)¶
Standard circuit element.
Keyword Arguments are equivalent to calling setter methods.
- Parameters:
d – Drawing direction (‘up’, ‘down’, ‘left’, ‘right’)
- 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
- Type:
ChainMap[str, Any]
Anchor names are dynmically added as attributes after placing the element in a Drawing.
- anchor(anchor: str) Element ¶
Specify anchor for placement. The anchor will be aligned with the position specified by at() method.
- at(xy: XY | tuple['Element', str], dx: float = 0, dy: float = 0) Element ¶
Set the element xy position
- Parameters:
xy – (x,y) position or tuple of (Element, anchorname)
- color(color: str) Element ¶
Sets the element color
- Parameters:
color – color name or hex value (ie ‘#FFFFFF’)
- drop(drop: str | Point) Element ¶
Set the drop position - where to leave the current drawing position after placing this element
- fill(color: bool | str = True) Element ¶
Sets the element fill color.
- Parameters:
color – Color string name or hex value, or
color. (True to fill with the element line) –
- 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)
- label(label: str | Sequence[str], loc: LabelLoc | None = None, ofst: XY | float | None = None, halign: Halign | None = None, valign: Valign | None = None, rotate: bool | float = False, fontsize: float | None = None, font: str | None = None, mathfont: str | None = None, color: str | None = None)¶
Add a label to the Element.
- Parameters:
label – The text string or list of strings. If list, each string will be evenly spaced along the element (e.g. [‘-’, ‘V’, ‘+’])
loc – Label position within the Element. Either (‘top’, ‘bottom’, ‘left’, ‘right’), or the name of an anchor within the Element.
ofst – Offset from default label position
halign – Horizontal text alignment (‘center’, ‘left’, ‘right’)
valign – Vertical text alignment (‘center’, ‘top’, ‘bottom’)
rotate – True to rotate label with element, or specify rotation angle in degrees
fontsize – Size of label font
font – Name/font-family of label text
mathfont – Name/font-family of math text
color – Color of label
- linestyle(ls: Literal['-', ':', '--', '-.']) Element ¶
Sets the element line style
- Parameters:
ls – Line style (‘-’, ‘:’, ‘–’, ‘-.’).
Element2Term¶
- class schemdraw.elements.Element2Term(*args, **kwargs)¶
Two terminal element. The element leads can be automatically extended to the start and ending positions.
- Anchors:
start
center
end
- dot(open: bool = False) Element2Term ¶
Add a dot to the end of the element
- endpoints(start: Tuple[float, float] | Point, end: Tuple[float, float] | Point) Element2Term ¶
Sets absolute endpoints of element
- idot(open: bool = False) Element2Term ¶
Add a dot to the input/start of the element
- length(length: float) Element2Term ¶
Sets total length of element
- to(xy: Tuple[float, float] | Point, dx: float = 0, dy: float = 0) Element2Term ¶
Sets ending position of element
- Parameters:
xy – Ending position of element
dx – X-offset from xy position
dy – Y-offset from xy position
- tox(x: float | XY | Element) Element2Term ¶
Sets ending x-position of element (for horizontal elements)
- toy(y: float | XY | Element) Element2Term ¶
Sets ending y-position of element (for vertical elements)
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(*args, **kwargs)¶
Element from an Image file
- Parameters:
image – Image filename or open file pointer
width – Width to draw image in Drawing
height – Height to draw image in Drawing
xy – 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: float = 3.0, inches_per_unit: float = 0.5, lblofst: float = 0.1, fontsize: float = 14.0, font: str = 'sans-serif', color: str = 'black', lw: float = 2.0, ls: Literal['-', ':', '--', '-.'] = '-', fill: str | None = None, bgcolor: str | None = None, margin: float = 0.1, mathfont: str | None = None) None ¶
Set global schemdraw style configuration
- Parameters:
unit – Full length of a 2-terminal element. Inner zig-zag portion of a resistor is 1.0 units.
inches_per_unit – Inches per drawing unit for setting drawing scale
lblofst – Default offset between element and its label
fontsize – Default font size for text labels
font – Default font family for text labels
color – Default color name or RGB (0-1) tuple
lw – Default line width for elements
ls – Default line style
fill – Deault fill color for closed elements
margin – White space around the drawing in drawing units
mathont – Font for math delimited by $..$
- 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: Literal['svg', 'matplotlib'] = 'matplotlib') None ¶
Change default backend, either ‘matplotlib’ or ‘svg’