1. 程式人生 > 實用技巧 >Python製作折線圖

Python製作折線圖

利用python的第三方包Pygal製作簡單的折線圖。

申明:本文僅供學習交流使用。原始碼大部分來自《python程式設計從入門到實踐》;如有侵權,請聯絡我刪除。

  1 #!usr/bin/env python3
  2 # -*-coding=utf-8 -*-
  3 '''
  4     製作一個簡單的交易走勢圖。原始碼大部分來自<python程式設計從入門到實踐>
  5     過程分析:
  6         1,從網站下載資料並存儲到本地,兩種方法:
  7             a,urlopen
  8             b,requests
  9         2,讀取本地資料,利用pygal製作交易走勢圖
10 ''' 11 #正文開始 12 from __future__ import (absolute_import, division, print_function, unicode_literals) 13 try: 14 from urllib2 import urlopen 15 except ImportError: 16 from urllib.request import urlopen 17 import json 18 import requests 19 import pygal 20 from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
21 22 #第一種方法利用urlopen提取網路上儲存的資料 23 #定義要提取資料的url 24 URL = "https://www.veecandy.com/btc_close_2017.json" 25 read_url = urlopen(URL) #讀取URL 26 read_response = read_url.read() #讀取返回的資料 27 28 #開始儲存資料到本地, 以'wb'形式(二進位制) 29 with open('btc_close_2017_urllib.json', 'wb') as file_object: 30 file_object.write(read_response) #
將讀取到的資料寫入到檔案 31 32 file_urllib = json.loads(read_response) 33 print(file_urllib) 34 35 #第二種方法利用requests提取網路上儲存的資料 36 #定義要提取資料的URL 37 URL = "https://www.veecandy.com/btc_close_2017.json" 38 response = requests.get(URL) #利用requests讀取URL並獲取資料 39 40 #開始儲存資料到本地檔案; 41 with open('btc_close_2017_requests.json', 'w') as file_object: 42 file_object.write(response.text) #寫入資料到本地檔案,要以str格式寫入 43 44 file_requests = response.json() 45 print(file_requests) 46 47 print("-----------------------I am delimiter-----------------") 48 #列印一下兩種方法是不是獲取到相同的資料 49 print(file_urllib == file_requests) #True 50 51 #開始讀取相關資料 52 filename = 'btc_close_2017.json'; #載入檔案 53 54 #以讀的方式開啟檔案 55 with open(filename) as file_object: 56 btc_data = json.load(file_object) #將讀取到的資料載入為json格式 57 58 print(btc_data) #列印是否符合預期 59 60 #列印每一天的資訊,並將字串轉換成數值型的: 61 for btc_dict in btc_data: 62 date = btc_dict['date'] 63 month = int(btc_dict['month']) 64 week = int(btc_dict['week']) 65 weekday = btc_dict['weekday'] 66 close_price = int(float(btc_dict['close'])) #先將字串轉換為浮點型,再將浮點型轉換成整型 67 print("{} is month: {}, week: {}, weekday: {}, the close price is: {}".format(date,month,week,weekday,close_price)) 68 69 70 #視覺化,開始繪製收盤價折線圖 71 72 #定義空列表以儲存迴圈得到的項 73 dates, months, weeks, weekdays, close_prices = [], [], [], [], [] 74 #迴圈讀取btc_data裡的每一項 75 for btc_dict in btc_data: 76 date = btc_dict['date'] 77 month = int(btc_dict['month']) 78 week = int(btc_dict['week']) 79 weekday = btc_dict['weekday'] 80 close_price = int(float(btc_dict['close'])) #先將字串轉換為浮點型,再將浮點型轉換成整型 81 82 #將每一項追加到對應的空列表 83 dates.append(date) 84 months.append(month) 85 weeks.append(week) 86 weekdays.append(weekday) 87 close_prices.append(close_price) 88 89 line_chart = pygal.Line(x_label_rotation=20,show_minor_x_labels=False) #x軸標籤順時針旋轉20度,show_minor_x_labels告訴pygal不必顯示全部的x軸標籤 90 line_chart.title = "Close Price ($)" #標題 91 line_chart.x_title = "Dates" #x軸標題 92 line_chart.y_title = "Price" #y軸標題 93 line_chart.x_labels = dates #x軸標籤 94 line_chart.x_labels_major = dates[::20] #讓x軸座標每隔20天顯示第一次; 95 line_chart.add("BTC", close_prices) # 96 line_chart.render_to_file('closeprice.svg') 97 98 ''' 99 寫一些折線圖的樣式 100 ''' 101 my_style = LS("#999888",base_style=LCS) 102 my_config = pygal.Config() 103 my_config.x_label_rotation = 20 104 my_config.show_legend = False 105 my_config.show_minor_x_labels = False 106 my_config.title_font_size = 10 107 my_config.label_font_size = 10 108 my_config.major_label_font_size = 18 109 my_config.truncate_label = 10 110 my_config.show_y_guides = False 111 my_config.width = 1000 112 113 #利用樣式視覺化 114 line_chart = pygal.Line(my_config,style=my_style) 115 line_chart.title = "BTC close price in 2017" 116 line_chart.x_title = "Dates" 117 line_chart.y_title = "Price" 118 line_chart.x_labels = dates 119 line_chart.x_labels_major = dates[::20] 120 line_chart.add("SPJ",close_prices) 121 line_chart.render_to_file("closeprice1.svg")