前言
到畢業季了,博主要寄行李,校園里很多快遞提出了自己優惠的服務,如何選擇最便宜的快遞服務呢?
思路
使用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這種快遞較為合適
聲明
本博客只是學習交流使用,沒有其他意圖。若有問題,歡迎大家指正,我看到后馬上改正。最后,畢業快樂!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
