python库使用笔记

文章目录

  • control的官方网站
    • 部分函数示例
      • 强迫响应forced_response
  • excel资源库
    • xlrd
  • 绘图设置画板背景
    • matplotlib.pyplot设置黑色背景
  • pyzero库自娱自乐
    • 网络教程资源
    • 库安装
    • demo(字体使用及文件路径)
      • 效果图
      • 工程配置
      • 代码示例

control的官方网站

部分函数示例

强迫响应forced_response

import numpy as np
import os
import sys
import control as ctrl
import matplotlib.pyplot as plt

def lim_x(x, lim=0):
    res = 0
    if x > lim:
        res = 1
    else:
        res = 0
    return res

if __name__ == "__main__":
    print(__file__)

    fc = 20
    Ts = 50*10e-6
    T = 1/np.pi/2/fc

    omega = 2*np.pi*5/2
    t = np.arange(0, 2, Ts)
    y = np.sin(omega*t)
    ys = [lim_x(k) for k in y]
    plt.close("all")
    plt.figure("响应函数")

    lowf = ctrl.tf([1], [T, 1])
    lowf_z = ctrl.c2d(lowf, Ts)
    tout, yout = ctrl.forced_response(lowf_z, T=t, U=ys, X0=0)
    plt.plot(tout, yout)
    plt.plot(t, ys)
    plt.legend(["input", "output"])
    plt.xlabel("time/s")
    plt.show()

    # print(lowf, lowf_z)

python库使用笔记_第1张图片

excel资源库

xlrd

# 安装指令
pip uinstall xlrd
# 2.0.1不能读取xlsx,回退旧版本
pip install xlrd==1.2.0

绘图设置画板背景

matplotlib.pyplot设置黑色背景

plt.style.use('dark_background')

python库使用笔记_第2张图片

pyzero库自娱自乐

网络教程资源

库安装

pip install pyzero
pip install pygames

demo(字体使用及文件路径)

效果图

python库使用笔记_第3张图片

工程配置


│  clock.py
│
├─.vscode
│      launch.json
│
├─fonts
│      simhei.ttf
│
└─images
        clock 0.png
        clock 1.png
        clock 2.png
        clock 3.png
        clock 4.png
        clock 5.png
        clock 6.png
        clock 7.png

代码示例

import pgzrun
import os, sys
import time
import datetime

WIDTH = 300
HEIGHT = 200
timestamp = ""
timenum = 0
HOUR_BASE = 8

WALKING  = '动动腿'
DRINKING = '喝口水'
MAX_PICNUM = 8
sec_num = 0
min_num = 0
hour_num = 0
babyidx = 0

def update_time():
    global timestamp
    global timenum
    timenum = time.time()
    timestamp = time.strftime("%Y/%m/%d %H:%M:%S",time.localtime(timenum))

def update():
    global timestamp
    global timenum
    global sec_num
    global min_num
    global hour_num
    global babyidx

    update_time()

    babyidx = int((timenum * 10) % MAX_PICNUM)
    sec_num = (timenum*10)%60
    min_num = (timenum // 60)%60
    hour_num = ((min_num // 60) + HOUR_BASE)%24
    pass

def draw():
    black = (0,0,0)
    screen.fill(black)
    screen.draw.text(timestamp, midbottom = [WIDTH/2, HEIGHT])
    fontsize0 = 26
    fontsize1 = 26
    if 0 <= min_num < 30:
        fontsize0 += sec_num%20
    elif 30 <= min_num:
        fontsize1 += sec_num%20
    timebaby[babyidx].draw()
    screen.draw.text(WALKING, bottomleft = [10, HEIGHT//5*1.6], fontname="simhei", fontsize=fontsize0, color = "red")
    screen.draw.text(DRINKING, bottomleft = [10, HEIGHT//5*2.6], fontname="simhei", fontsize=fontsize1, color = "orange")
    pass

TITLE = "开心工作数十年"
timebaby = []
for k in range(MAX_PICNUM):
    timebaby.append(Actor("clock "+str(k), bottomleft = [WIDTH*0.6, HEIGHT/3*2]))
pgzrun.go()

你可能感兴趣的:(python,开发语言,control,控制系统,仿真,plot,pyzero)