1. 程式人生 > >【資料分析】:python:金融資料指標分析

【資料分析】:python:金融資料指標分析

python:金融資料指標分析

# -*- coding: utf-8 -*-
"""
Created on Wed Jul  4 17:31:47 2018

@author: 孫正陽
"""
#@匯入功能模組資料包
import numpy as np
import pandas as pd 
#import matplotlib.pyplot as plt
#plt.rcParams["font.sans-serif"] = ["SimHei"]
#plt.rcParams['axes.unicode_minus'] = False
import warnings 
warnings.filterwarnings('ignore')

import os
os.chdir('C:/Users/A3/Desktop/')
df= pd.read_excel('指標畫圖.xlsx',sheetname = 0,header = 0,index_col = [0])
data = pd.DataFrame(df)
#==============================================================================
# timeStamp = df['time']
# tm = time.strptime(timeStamp, '%Y-%m-%d %H:%M:%S')
# tm
# timeArray = time.localtime(timeStamp)
# otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
# otherStyleTime   # 2013--10--10 23:40:00
#==============================================================================
from bokeh.plotting import figure,show,output_file 
from bokeh.models import ColumnDataSource
from bokeh.models import HoverTool
from bokeh.layouts import gridplot
from bokeh.models.annotations import Span
from bokeh.models.annotations import Label
#from bokeh.layouts import gridplot

#資料統計
source = ColumnDataSource(data)
time_lst = data.index.tolist()
#建立資料
output_file('weight_add.html') 
hover = HoverTool(tooltips = [('指標',"@整體首逾率")])
p = figure(
           plot_width = 1000,
           plot_height =450,
           title = '音速分期C~M1%指標走勢',
           x_axis_label = '時間趨勢', 
           y_axis_label = 'C~M1%', 
           x_range = time_lst,
           y_range = [0,0.3],
           tools = [hover,'pan,wheel_zoom,box_zoom,save,reset,help'],# 工具欄位置
           toolbar_location='above'    # 工具欄位置:"above","below","left","right")
           )

p.line(x = 'time',
       y = '整體首逾率',
       source = source,
       legend = 'C~M1%-時間線',
       line_width = 1,
       line_alpha = 0.8,
       line_color = 'black',
       line_dash = [10,4])
p.circle(x = 'time',
         y = '整體首逾率',
         source = source,
         size = 10,
         color = 'green',
         alpha = 0.8)
p.title.text_color = "white"  #顏色
p.title.text_font = "times"   #字型
p.title.text_font_style = "italic"  #風格
p.title.text_font_size= '18pt'
p.title.background_fill_color = "black"  #背景顏色

# 設定標題

p.outline_line_width = 5         # 邊框線寬
p.outline_line_alpha = 0.3       # 邊框線透明度
p.outline_line_color = "green"    # 邊框線顏色

p.background_fill_color = "beige"    # 繪圖空間背景顏色
p.background_fill_alpha = 0.5        # 繪圖空間背景透明度

p.border_fill_color = "whitesmoke"    # 外邊界背景顏色
p.min_border_left = 80                # 外邊界背景 - 左邊寬度
p.min_border_right = 80               # 外邊界背景 - 右邊寬度
p.min_border_top = 30                 # 外邊界背景 - 上寬度
p.min_border_bottom = 10              # 外邊界背景 - 下寬度
#設定邊界背景

p.yaxis.axis_line_width = 2  #線寬
p.yaxis.axis_line_color = "red"  #軸線顏色
# 設定軸線
p.axis.minor_tick_in = 5      # 刻度往繪圖區域內延伸長度
p.axis.minor_tick_out = 3   # 刻度往繪圖區域外延伸長度
# 設定刻度
#p.yaxis.bounds = (86, 100)
# 設定軸線範圍

p.xaxis.axis_label = "時間"
p.xaxis.axis_label_text_color = "#aa6666"
p.xaxis.axis_label_standoff = 30
# 設定標籤名稱、字型顏色、偏移距離

p.yaxis.axis_label = "C~M1%"
p.yaxis.axis_label_text_font_style = "italic"
# 設定標籤名稱、字型

p.yaxis.major_label_text_color = "orange"
p.yaxis.major_label_orientation = "vertical"
# 設定y軸線:標籤、字型顏色、字型角度

p.xgrid.grid_line_color = None
# 顏色設定,None時則不顯示

p.ygrid.grid_line_alpha = 0.2
p.ygrid.grid_line_dash = [6, 4]
# 設定透明度,虛線設定
# dash → 通過設定間隔來做虛線
# minor_line → 設定次軸線
p.xgrid.minor_grid_line_color = 'navy'
p.xgrid.minor_grid_line_alpha = 0.1
#p = gridplot([p])
p.grid.bounds = (-1, 1)
# 設定填充邊界

p.legend.location = "top_left"
# 設定圖例位置:"top_left"、"top_center"、"top_right" (the default)、"center_right"、"bottom_right"、"bottom_center"
# "bottom_left"、"center_left"、"center"

p.legend.orientation = "vertical"
# 設定圖例排列方向:"vertical" (預設)or "horizontal"

p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"  # 斜體
p.legend.label_text_color = "navy"
p.legend.label_text_font_size = '12pt'
# 設定圖例:字型、風格、顏色、字型大小

p.legend.border_line_width = 2
p.legend.border_line_color = "navy"
p.legend.border_line_alpha = 0.5
# 設定圖例外邊線:寬度、顏色、透明度

p.legend.background_fill_color = "gray"
p.legend.background_fill_alpha = 0.2
# 設定圖例背景:顏色、透明度

upper = Span(location=0.1,           # 設定位置,對應座標值
             dimension='width',    # 設定方向,width為橫向,height為縱向  
             line_color='firebrick',
             line_width=1,# 設定線顏色、線寬
             line_dash = [5,2])
p.add_layout(upper)
# 繪製輔助線1
lower = Span(location=0.05, 
             dimension='width', 
             line_color='olive', 
             line_width=1,
             line_dash = [5,2])
p.add_layout(lower)
# 繪製輔助線2

#==============================================================================
# label1 = Label(x=6, y=86,       # 標註註釋位置
#               x_offset=12,    # x偏移,同理y_offset
#               text="跑了10km",      # 註釋內容
#               text_font_size="12pt",    # 字型大小
#               border_line_color="green", 
#               background_fill_color="gray", 
#               background_fill_alpha = 0.5   # 背景線條顏色、背景顏色、透明度
#              )
# p.add_layout(label1)
# label2 = Label(x=7, y=87.5,       # 標註註釋位置
#               x_offset=12,    # x偏移,同理y_offset
#               text="吃了不少高熱量食物",      # 註釋內容
#               text_font_size="12pt",    # 字型大小
#               border_line_color="red", 
#               background_fill_color="gray", 
#               background_fill_alpha = 0.5   # 背景線條顏色、背景顏色、透明度
#              )
# p.add_layout(label2)
# label3 = Label(x=11, y=86,       # 標註註釋位置
#               x_offset=12,    # x偏移,同理y_offset
#               text="跑步5km",      # 註釋內容
#               text_font_size="12pt",    # 字型大小
#               border_line_color="green", 
#               background_fill_color="gray", 
#               background_fill_alpha = 0.5   # 背景線條顏色、背景顏色、透明度
#              )
# p.add_layout(label3)
# label4 = Label(x=13, y=87,       # 標註註釋位置
#               x_offset=12,    # x偏移,同理y_offset
#               text="晚餐大量進食",      # 註釋內容
#               text_font_size="12pt",    # 字型大小
#               border_line_color="red", 
#               background_fill_color="gray", 
#               background_fill_alpha = 0.5   # 背景線條顏色、背景顏色、透明度
#              )
# p.add_layout(label4)
#==============================================================================
# 繪製註釋


#p = gridplot([p])

show(p)               

print('finished!')