当前第2页 返回上一页
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
def sgn(value):
if value < 4:
return 20
else :
return 15
plt.figure(figsize=(6, 4))
x = np.linspace(0, 8, 100)
y = np. array ([])
for v in x:
y = np.append(y, np.linspace(sgn(v), sgn(v), 1))
l = plt.plot(x, y, 'b' , label= 'type' )
plt.legend()
plt.show()
|


3.绘制三角波形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
def triangle_wave(x, c, c0, hc):
x = x - int(x) #三角波周期为1 因此只取小数部分进行计算
if x < c0:
return x / c0 * hc
elif x >= c:
return 0.0
else :
return (c-x)/(c-c0)*hc
x = np.linspace(0, 2, 1000)
y = np. array ([triangle_wave(t, 0.6, 0.4, 1.0) for t in x])
plt.figure()
plt.plot(x, y)
plt.ylim(-0.2, 1.2) #限制y的范围
plt.show()
|


推荐教程:《python视频教程》
以上就是python分段函数如何编写?的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python如何批量提取win10锁屏壁纸
Python之property()装饰器的使用详解
Python输出语句print的用法是什么?
Python elif是什么意思
Python中range函数怎么用
Python不能做什么
详解Python中and和or的返回值
Python调用xlsxwriter创建xlsx的方法
Python实现逐行分割大txt文件的方法介绍
Python爬虫基础之网页组成解析
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python分段函数如何编写?