'''
x y
X<0 0
0≤x<5 x
5≤x<10 3x-5
10≤x<20 0.5x-2
20≤x 0
'''
import matplotlib.pyplot as plt #数据可视化模块
import numpy as np
def f(x):
if x<0:
return 0
elif x>=0 and x<5:
return x
elif x>=5 and x<10:
return 3*x-5
elif x>=10 and x<20:
return (0.5)*x-2
else:
return 0
x=float(input("x=:"))
print("f(",x,")=",f(x))
#绘图
x=np.linspace(-10,50,1000)
y=[f(x) for x in x]
plt.figure()
plt.title("y=f(x)")
plt.plot(x,y)
plt.show()
运行结果