前言
到毕业季了,博主要寄行李,校园里很多快递提出了自己优惠的服务,如何选择最便宜的快递服务呢?
思路
使用python中matplotlib工具直接绘图,直观看出哪家快递更适合自己
代码
import math
import numpy as np
import matplotlib.pyplot as plt
def sunfeng(x): #分段函数 低于40斤均为30元 高出部分使用0.9元/斤
money = []
for x_num in x: #这里注意一下由于x是list,因此需要循环,而python公式内部直接做了处理,因此不用for循环,公式输入为list,输出同样为list,matplotlib绘制x和y也均为列表或者numpy的array
if x_num<40:
money.append(30)
else:
money.append(30 + 0.9*(x_num-40))
return money
def yunda(x): # 1元/斤
return x
def guotong(x): # 分享朋友圈 打九折 也就是0.9元/斤
return 0.9*x
if __name__ == '__main__':
x = np.arange(0,150,0.1)
y1 = sunfeng(x)
y2 = yunda(x)
y3 = guotong(x)
plt.xlim(0,150)
plt.ylim(0,200)
plt.plot(x,y1,label='sunfeng',linestyle="-", color="red")
plt.plot(x,y2,label='yunda',linestyle="-", color="green")
plt.plot(x,y3,label='guotong_has_if',linestyle="-", color="blue")
plt.legend(['sunfeng','yunda','guotong_has_if'])
plt.savefig('plot_test.png', dpi=100)
plt.show()
结果
结论
由于博主包裹很多超过了40斤,选择sunfeng这种快递较为合适
声明
本博客只是学习交流使用,没有其他意图。若有问题,欢迎大家指正,我看到后马上改正。最后,毕业快乐!