python实用库之schemdraw不只是绘制原理图

主要信息来自官网

Getting Started — SchemDraw 0.14 documentation

1. 了解元器件

基本元件:阻容感,开关,插座,运放,电缆,变压器,基本够用了? 

库里的芯片: 逻辑芯片,数码管, DIP封装片,预定义芯片

连接器:单排,双排,跳线帽,DB头,数据总线,电源插座

 组合原件:光耦,继电器,桥堆

 逻辑电路: 门电路,组合逻辑, 真值表,

 时序图: 信号时序

 信号处理:可以认为来表示DSP的处理能力

流程图及框图:流程图,连接线,状态图,决策图,

 电路库及其丰富,包括电路示意图等等

2. 基本操作

place 放置元件

with schemdraw.Drawing() as d:
    d += elm.Capacitor()
    d += elm.Resistor()
    d += elm.Diode()

输出: 

方向选择, 放置时增加方向的methods就可以改变防止方向,并且直到下次改变

    d += elm.Capacitor()
    d += elm.Resistor().up()
    d += elm.Resistor()
    d += elm.Diode().right()

  输出结果: 

        

theta方法可以设置偏转角度并保持,注意这里角度是绝对值:

    d += elm.Capacitor()
    d += elm.Resistor().up()
    d += elm.Resistor()
    d += elm.Diode().right()

    d +=elm.Resistor().theta(45)
    d +=elm.Resistor()
    d +=elm.Resistor().theta(135)

3. 各种连接方式

按方向(up,down,lef,right),按起点终点(at, to), 按端口,按x,y(tox,toy), 封闭(endpoints)

多连接, dot, push,pop

指定位置:drop, hold, here

at方法指定器件起始端连接引脚

    d += (opamp := elm.Opamp())
    
    d += elm.Resistor().left().at(opamp.in2).label("R1")
    d += elm.Resistor().at(opamp.in1).label("R2")
    d += elm.Resistor().right().at(opamp.out).label("R3")

dot方法打连接点, length方法增加长度

    d += elm.Dot()
    d += elm.Resistor()
    d += elm.Dot()
    d += elm.Diode().length(6)
    d += elm.Dot()

to方法指定末端连接位置

 

    R = d.add(elm.Resistor())
    C = d.add(elm.Capacitor().up())
    Q = d.add(elm.Diode().to(R.start))

 tox和toy方法用于划线到指定位置,其实是to x 和to y

        d += (C := elm.Capacitor())
        d += elm.Diode()
        d += elm.Line().down()
        d += elm.Line().tox(C.start) # line to the x value of start of C
        d += elm.Resistor().up()

 endpoints方法更直接,在两个指定端点之间放置元件

        d += (R := elm.Resistor())
        d += (Q := elm.Diode().down(6))
        d += elm.Line().tox(R.start)
        d += elm.Capacitor().toy(R.start)
        d += elm.SourceV().endpoints(Q.end, R.start)

 控制翻转: flip 左右, reverse 上下

 

        d += (opamp := elm.Opamp())
        d += elm.Zener().left().label('Normal').at(opamp.in1)
        d += elm.Zener().left().flip().label('Flip').at(opamp.in2)

 push和pop方法用于配置绘制方向,可在连接点用于多个方向连接时的绘制

        d += elm.Inductor()
        d += elm.Dot()

        d.push()  # Save this drawing position/direction for later

        d += elm.Capacitor().down()  # Go off in another direction temporarily
        d += elm.Ground(lead=False)


        d.pop()   # Return to the pushed position/direction

        d += elm.Diode()

 drop & hold 保持不移动

        d += elm.Diode()  # Normal placement: drawing position moves to end of element
        d += elm.Dot().color('red')

        d.here = (0, -1)
        d += elm.Diode().hold()  # Hold method prevents position from changing
        d += elm.Dot().color('blue')

 一个元件的位置坐标值为1

疑问: 网格是怎么定义的? 

3. 配置参数,设置label

表达方式:label支持utf-8格式的字符串, 支持LaTeX数学公式表达方式

数学表达式的具体表达,可参考如下连接

Writing mathematical expressions — Matplotlib 3.5.1 documentation

with schemdraw.Drawing() as d:
    d += elm.Resistor().label('1MΩ')
    d += elm.Capacitor().label('1μF')
    d += elm.Capacitor().label(r'$v = \frac{1}{C} \int i dt$')
    d += elm.Resistor().at((0, -2)).label('$R_0$')
    d += elm.Capacitor().label('$x^2$')

放置位置, loc:top, bottom,right, left

    d += elm.Resistor().label('1MΩ')
    d += elm.Capacitor().label('1μF')
    d += elm.Capacitor().label(r'$v = \frac{1}{C} \int i dt$')
    d += elm.Resistor().at((0, -2)).label('$R_0$')
    d += elm.Capacitor().label('$x^2$')
    # label for location
    
    d += (elm.Resistor().at((0,-4))
        .label('Label')  # 'top' is default
        .label('Bottom', loc='bottom')
        .label('Right', loc='right')
        .label('Left', loc='left'))

放置位置也可以跟随角点走

    # label for value
    d += elm.Resistor().label('1MΩ')
    d += elm.Capacitor().label('1μF')
    d += elm.Capacitor().label(r'$v = \frac{1}{C} \int i dt$')
    d += elm.Resistor().at((0, -2)).label('$R_0$')
    d += elm.Capacitor().label('$x^2$')
    # label for location
    
    d += (elm.Resistor().at((0,-4))
        .label('Label')  # 'top' is default
        .label('Bottom', loc='bottom')
        .label('Right', loc='right')
        .label('Left', loc='left'))

    d += (elm.BjtNpn().at((0,-6))
        .label('b', loc='base')
        .label('c', loc='collector')
        .label('e', loc='emitter'))

 也可以控制label的走向,位移,字体,排版和颜色

    d += elm.Resistor().label('no offset').at((0,-10))
    d += elm.Resistor().label('offset', ofst=1)
    d += elm.Resistor().label('offset (x, y)', ofst=(-.6, .2))
    d += elm.Resistor().theta(-45).label('no rotate')
    d += elm.Resistor().theta(-45).label('rotate', rotate=True)
    d += elm.Resistor().theta(45).label('90°', rotate=90, color="red")

 电压类的label, 可以用gap元素来作为截断点

   # label for gap
    d += elm.Line().dot(open=True)
    d += elm.Gap().label(('–','$V_o$','+'))
    d += elm.Line().idot(open=True)
    

 电流箭头label ,加reverse()方法可以调转方向

# label for voltage
    d += elm.Resistor().label(('-','$R_1$','+')) 
    
    # label for gap
    d += elm.Line().dot(open=True)
    d += elm.Gap().label(('–','$V_o$','+'))
    d += elm.Line().idot(open=True)

    R1 = d.add(elm.Resistor().down())
    d += elm.CurrentLabel().at(R1).label('10 mA').reverse()
    
   

线内的电流箭头:

# label for voltage
    d += elm.Resistor().label(('-','$R_1$','+')) 
    
    # label for gap
    d += elm.Line().dot(open=True)
    d += elm.Gap().label(('–','$V_o$','+'))
    d += elm.Line().idot(open=True)

    R1 = d.add(elm.Resistor().down())
    d += elm.CurrentLabel().at(R1).label('10 mA').reverse()
    
    d+=elm.Line(3).left()
    R2 = d.add(elm.Resistor().left())
    d += elm.CurrentLabelInline(direction='in').at(R2).label('10 mA')
   

Loop Current 电流环

    # loop current
    R1 = d.add(elm.Resistor().label("R1"))
    C1 = d.add(elm.Capacitor().label("C1").down())
    D1 = d.add(elm.Diode().label("D1",loc='bottom').fill(True).left())
    L1 = d.add(elm.Inductor().label("L1").up())
    d += elm.LoopCurrent([R1, C1, D1, L1], direction='cw').label('$I_1$')
 

 非常贴心的功能,批注Annotations

# loop current
    R1 = d.add(elm.Resistor().label("R1"))
    C1 = d.add(elm.Capacitor().label("C1").down())
    D1 = d.add(elm.Diode().label("D1",loc='bottom').fill(True).left())
    L1 = d.add(elm.Inductor().label("L1").up())
    d += elm.LoopCurrent([R1, C1, D1, L1], direction='cw').label('$I_1$')
 
    parallel = d.add(elm.Encircle([R1], padx=.8).linestyle('--').linewidth(1).color('red'))
    series = d.add(elm.Encircle([C1], padx=.8).linestyle('--').linewidth(1).color('blue'))

    d += elm.Annotate().at(parallel.NNE).delta(dx=1, dy=1).label('R1 annotation').color('red')
    d += elm.Annotate(th1=0).at(series.ENE).delta(dx=1.5, dy=1).label('D1 annotation').color('blue')
    

4. style 样式

元件样式包括: color, fill, linewidth ,linestyle ,未单独定义情况下,数值来自global defaults缺省值

with schemdraw.Drawing(color='blue', fill='lightgray') as d:


# All elements are blue with lightgray fill unless specified otherwise
   

    d += elm.Diode()
    d += elm.Diode().fill('red')        # Fill overrides drawing color here
    d += elm.Resistor().fill('purple')  # Fill has no effect on non-closed elements
    d += elm.RBox().linestyle('--').color('orange')
    d += elm.Resistor().linewidth(5)

config方法可以定义全局样式

schemdraw.config(lw=1, font='serif')
with schemdraw.Drawing() as d:
    d += elm.Resistor().label('100KΩ')
    d += elm.Capacitor().down().label('0.1μF', loc='bottom')
    d += elm.Line().left()
    d += elm.Ground()
    d += elm.SourceV().up().label('10V')

 设置风格(背景? )

Themes

Schemdraw also supports themeing, to enable dark mode, for example. The defined themes match those in the Jupyter Themes package:

  • default (black on white)

  • dark (white on black)

  • solarizedd

  • solarizedl

  • onedork

  • oceans16

  • monokai

  • gruvboxl

  • gruvboxd

  • grade3

  • chesterish

 测试了几个样式 .....

onedork

solarizedd

monokai

 5.  backend 相当于画布canvas

 就是两样: matplotlib和SVG, 默认matplotlib, 配置成svg可以直接被网页引用,更方便,尤其是不需要单独调入字体。

选择依据,参见官方说明: 

Reasons to choose the SVG backend include:

  • No Matplotlib/Numpy dependency required (huge file size savings if bundling an executable).

  • Speed. The SVG backend draws 4-10x faster than Matplotlib, depending on the circuit complexity.

Reasons to use Matplotlib backend:

  • To customize the schematic after drawing it by using other Matplotlib functionality.

  • To render directly in other, non-SVG, image formats, with no additional code.

5. 常用模块电路

6. 定制部件

Customizing Elements

群组复制

7. 输出SVG图片

加入backend定义:

with schemdraw.Drawing(backend="svg") as d:

然后用d.save(path)来保存svg图片

Matplotlib 是 Python 中常用的绘图之一,可以用于绘制各种类型的图形。以下是 Matplotlib 绘制常见图形的基本原理和设计说明: 1. 折线图 折线图用于展示数据随时间或其他变量的变化趋势。绘制折线图的基本原理是通过 `plt.plot()` 函数绘制数据点的连续线条。在绘图时,可以设置线条的颜色、线型、标记点等属性,以便更好地展示数据。 2. 散点图 散点图用于展示数据点之间的关系。绘制散点图的基本原理是通过 `plt.scatter()` 函数绘制数据点的位置和颜色等属性。在绘图时,可以设置数据点的大小、颜色、标记形状等属性,以便更好地展示数据。 3. 条形图 条形图用于展示不同类别之间的数据比较。绘制条形图的基本原理是通过 `plt.bar()` 函数绘制每个类别的数据条。在绘图时,可以设置条形的宽度、颜色、边框等属性,以便更好地展示数据。 4. 饼图 饼图用于展示各类别数据所占比例。绘制饼图的基本原理是通过 `plt.pie()` 函数绘制每个类别的数据所占比例。在绘图时,可以设置饼图的大小、颜色、标签等属性,以便更好地展示数据。 5. 直方图 直方图用于展示数据的分布情况。绘制直方图的基本原理是通过 `plt.hist()` 函数绘制数据的频率直方图。在绘图时,可以设置直方图的宽度、颜色、边界、标签等属性,以便更好地展示数据。 总的来说,Matplotlib 绘制常见图形的基本原理是通过调用各种绘图函数,设置各种绘图属性,以便更好地展示数据。在使用 Matplotlib 绘图时,需要对不同类型的图形有一定的了解,在实际绘制时需要根据数据特点和展示目的进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海里的鱼2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值