1 2 3 4 5 6 7 8 9 10 | import requests import json url = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?date=%s&stockNo=%s' % ( '20181201', '2892') r = requests.get(url) print(r.text) data =json.loads(r.text) print(data['title']) print(data['fields']) for row in data['data']: print(row) |
2019年11月16曾介紹pyecharts套件的使用,在"初體驗! 使用pyecharts以水果交易行情為例"文章中,僅9行程式就能畫出台北一和台北二果菜場的交易,其程式如下:
from pyecharts.charts import Bar from pyecharts import options as opts from pyecharts.globals import ThemeType bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(["椪柑", "蘋果 富士進口", "番石榴 珍珠芭", "香蕉", "木瓜 網室紅肉", "甜橙 柳橙", "火龍果 紅肉"]) bar.add_yaxis("台北一", [29.3, 51.5, 40.3, 21.6, 24.0, 61.5, 34.2]) bar.add_yaxis("台北二", [32.3, 51.4, 53.7, 24.7, 42.3, 23.4, 35.9]) bar.set_global_opts(title_opts=opts.TitleOpts(title="平均價", subtitle="元")) bar.render()
我們把兩支程式合併,運用串列資料把股票交易日期當成x軸,再把開盤價、最高價、'最低價、以及'收盤價當成y軸來繪製圖表,其程式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import requests import json url = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?date=%s&stockNo=%s' % ( '20181201', '2892') r = requests.get(url) print(r.text) data =json.loads(r.text) print(data['title']) print(data['fields']) price=data['fields'][3:7] xaxis=[] yaxis=[[],[],[],[]] for row in data['data']: print(row) xaxis.append(row[0]) for i, y in enumerate(yaxis): y.append(row[i+3]) from pyecharts.charts import Bar from pyecharts import options as opts from pyecharts.globals import ThemeType bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(xaxis) for i, y in enumerate(yaxis): bar.add_yaxis(price[i], y) bar.set_global_opts(title_opts=opts.TitleOpts(title="平均價", subtitle="元")) bar.render() |
執行結果:
沒有留言:
張貼留言