資料視覺化學習--------------------下載資料(二)
阿新 • • 發佈:2018-12-06
上章是講得csv,這節是json
1.如何下載資料:
以下程式碼是利用模組urllib函式urlopen()下載資料
#-*-coding:GBK-*- #-*-coding:utf-8-*- from __future__ import (absolute_import,division,print_function,unicode_literals) try: from urllib2 import urlopen except ImportError: from urllib.request import urlopen import json json_url='https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json' response=urlopen(json_url) #讀取資料 req=response.read() #將資料寫入檔案 with open('btc_close_2017_urllib.json','wb') as f: f.write(req) #載入json格式 file_urllib=json.loads(req) print(file_urllib)
還可以使用第三模組request。
#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import requests
json_url='https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
req=requests.get(json_url)
with open('btc_close_2017_request.json','w') as f:
f.write(req.txt)
file_requests=req.json()
提取相關的資料:
#-*-coding:GBK-*- #-*-coding:utf-8-*- import json filename='btc_close_2017.json' with open(filename) as f: btc_data=json.load(f) #列印每天的訊息 for btc_dict in btc_data: date=btc_dict['date'] month=btc_dict['month'] week=btc_dict['week'] weekday=btc_dict['weekday'] close=btc_dict['close'] print("{} is month {}week{},{},the close price is {}RMB".format(date,month,week,weekday,close))
匯入json模組,開啟檔案,把資料載入在btc_data裡,遍歷它
將字元型裝變為數字值:
#-*-coding:GBK-*- #-*-coding:utf-8-*- import json filename='btc_close_2017.json' with open(filename) as f: btc_data=json.load(f) #列印每天的訊息 for btc_dict in btc_data: date=btc_dict['date'] month=int(btc_dict['month']) week=int(btc_dict['week']) weekday=btc_dict['weekday'] close=int(float(btc_dict['close'])) print("{} is month {} week {},{},the close price is {}RMB".format(date,month,week,weekday,close))
繪製折線圖:
#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import json
import pygal
filename='btc_close_2017.json'
with open(filename) as f:
btc_data=json.load(f)
#列印每天的訊息
dates=[]
months=[]
weeks=[]
weekdays=[]
closes=[]
for btc_dict in btc_data:
dates.append(btc_dict['date'])
months.append(int(btc_dict['month']))
weeks.append(int(btc_dict['week']))
weekdays.append(btc_dict['weekday'])
closes.append(int(float(btc_dict['close'])))
#視覺化處理
line_chart=pygal.Line(x_label_rotation=20,show_minor_x_labels=False)
line_chart.title='收盤價'
line_chart.x_labels=dates
N=20#x軸每隔20天顯示一次
line_chart.x_labels_major=dates[::N]
line_chart.add('收盤價',closes)
line_chart.render_to_file("收盤價折線圖.svg")
再對資料進行數學分析,找找規律
對資料進行線性分析,做對數變換
#-*-coding:GBK-*-
#-*-coding:utf-8-*-
import json
import pygal
import math
filename='btc_close_2017.json'
with open(filename) as f:
btc_data=json.load(f)
#列印每天的訊息
dates=[]
months=[]
weeks=[]
weekdays=[]
closes=[]
for btc_dict in btc_data:
dates.append(btc_dict['date'])
months.append(int(btc_dict['month']))
weeks.append(int(btc_dict['week']))
weekdays.append(btc_dict['weekday'])
closes.append(int(float(btc_dict['close'])))
#視覺化處理
line_chart=pygal.Line(x_label_rotation=20,show_minor_x_labels=False)
line_chart.title='收盤價對數變換'
line_chart.x_labels=dates
N=20#x軸每隔20天顯示一次
line_chart.x_labels_major=dates[::N]
closes_log=[math.log10(_) for _ in closes]
line_chart.add('log收盤價',closes_log)
line_chart.render_to_file("收盤價折線圖.svg")