天天看點

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

卻發現他是這麼拿我當兄弟的

事情的經過是這樣的:

我開開心心的去一家燒餅店吃飯

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

擡頭一看,二師兄又漲價了

歎了口氣,再這麼下去真的要吃不起夾肉的燒餅了

點了兩個燒餅一碗馄饨

快吃完的時候,收到了朋友阿東的微信

阿東是我國中同學,好些日子沒聯系了

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

眼瞅着他快聊到區塊鍊了,雖然這事不靠譜,但還是答應了阿東幫他做圖。

我趕緊吃完最後一口,回公司,決定不午睡了。

開工!

阿東想要這樣的圖:

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

折線圖嘛,先擷取資料吧

正好圖檔中顯示了資料來源:“中國養豬網”

打開官網,找到并跳轉到豬價網址 http://zhujia.zhuwang.cc/

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

F12,Network檢視異步請求XHR,成功找到價格接口。

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

新标簽頁打開,線上解析Json資料,不了解這塊知識的朋友可以點選 在python裡玩轉Json資料

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

部分爬蟲代碼(完整代碼見文末下載下傳位址):

def get_comments(url):
    doc = get_json(url)
    dic = {}
    dic['pigprice'] = doc['pigprice']
    dic['pig_in'] = doc['pig_in']
    dic['pig_local'] = doc['pig_local']
    dic['maizeprice'] = doc['maizeprice']
    dic['bean'] = doc['bean']
    a = '-'.join(doc['time'][3])
    b = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    print(dateRange(a,b))
    dic['time'] = dateRange(a,b)
    return pd.DataFrame(dic)

data =get_comments('http://zhujia.zhuwang.cc/index/api/chartData?areaId=-1&aa=1571997555296')
           

運作結果:

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

成功擷取最近一年的各種價格√

下面開始給阿東做折線圖:

生豬價格的話選第一個外三元就好了,

使用 matplotlib 輕松可以做出折線圖。

from pylab import mpl
import  matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定預設字型
mpl.rcParams['axes.unicode_minus']

plt.figure(figsize=(8,4), dpi=80)
plt.plot(data['time'],data['pigprice'], color="r",linestyle = "-")
plt.xticks(data['time'][2::121], rotation=0)
plt.xlabel("生豬(外三元) 元/公斤")
           
python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

感覺還差點東西,可以标注一下最新一天的價格

另外既然已經擷取了玉米和豆粕的價格,就一起做了吧~

利用 plt.subplot 可以繪制多個子圖

from pylab import mpl
import  matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定預設字型
mpl.rcParams['axes.unicode_minus']

plt.figure(figsize=(8,10), dpi=80)
plt.figure(1)
ax1 = plt.subplot(311)
plt.plot(data['time'],data['pigprice'], color="r",linestyle = "-")
plt.xticks([])
plt.annotate(data['pigprice'][365], xy=(data['time'][365], 40), xytext=(data['time'][270], 35), arrowprops=dict(facecolor='black', shrink=0.1, width=0.5))
plt.xlabel("生豬(外三元) 元/公斤")

ax2 = plt.subplot(312)
plt.plot(data['time'],data['maizeprice'],color="y",linestyle = "-")
plt.xticks([])
plt.xlabel("玉米(15%水分) 元/噸")

ax3 = plt.subplot(313)
plt.plot(data['time'],data['bean'],color="g",linestyle = "-")
plt.xlabel("豆粕(43%蛋白) 元/噸")
plt.xticks(data['time'][2::121], rotation=0)
           
python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

完工,

發給阿東。

卻得到這樣的回複

python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。
python subplot_我用Python幫朋友做了張豬肉資料分析圖,結果。。。

原來在他心裡是這麼定義朋友的啊。

可以,

這很東哥,

這很兄弟。

一鍵爬取最新豬價&可視化的代碼已上傳github:

https://github.com/zpw1995/aotodata/tree/master/interest/pig

往期熱門,好玩有趣:

震驚,區塊鍊崗位薪資這麼高,Python爬取300個區塊鍊崗位深度分析,龍虎榜出爐!

手把手教你,菜鳥也能用Python寫一個2048遊戲

用Python做個海量小姐姐素描圖

Python裡三個最高逼格的調試神器