日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

利用python numpy+matplotlib繪制股票k線圖的方法

系統(tǒng) 1872 0

一、python numpy + matplotlib 畫股票k線圖

            
# -- coding: utf-8 --
import requests
import numpy as np  
from matplotlib import pyplot as plt  
from matplotlib import animation
 
fig = plt.figure(figsize=(8,6), dpi=72,facecolor="white")
axes = plt.subplot(111)
axes.set_title('Shangzheng')
axes.set_xlabel('time')
line, = axes.plot([], [], linewidth=1.5, linestyle='-')
alldata = []
 
def dapan(code):
	url = 'http://hq.sinajs.cn/?list='+code
	r = requests.get(url)
	data = r.content[21:-3].decode('gbk').encode('utf8').split(',')
	alldata.append(data[3])
	axes.set_ylim(float(data[5]), float(data[4]))
	return alldata
 
def init():
	line.set_data([], [])
	return line
 
def animate(i): 
 	axes.set_xlim(0, i+10)
 	x = range(i+1)
 	y = dapan('sh000001')
 	line.set_data(x, y)
 	return line
 
anim=animation.FuncAnimation(fig, animate, init_func=init, frames=10000, interval=5000)
 
plt.show()

          

二、使用matplotlib輕松繪制股票K線圖

K線圖是看懂股票走勢的最基本知識,K線分為陰線和陽線,陰線和陽線都包含了最低價、開盤價、最高價和收盤價,一般都K線如下圖所示:

利用python numpy+matplotlib繪制股票k線圖的方法_第1張圖片

在使用Python進(jìn)行股票分析的過程中,我們可以很容易的對K線圖進(jìn)行繪制,下面介紹兩種情形下的K線圖繪制:

1. 股票數(shù)據(jù)來源于Matplotlib:

            
# 導(dǎo)入需要的庫
import tushare as ts
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
 
%matplotlib inline
 
# 設(shè)置歷史數(shù)據(jù)區(qū)間
date1 = (2014, 12, 1) # 起始日期,格式:(年,月,日)元組
date2 = (2016, 12, 1) # 結(jié)束日期,格式:(年,月,日)元組
# 從雅虎財經(jīng)中獲取股票代碼601558的歷史行情
quotes = mpf.quotes_historical_yahoo_ohlc('601558.ss', date1, date2)
 
# 創(chuàng)建一個子圖 
fig, ax = plt.subplots(facecolor=(0.5, 0.5, 0.5))
fig.subplots_adjust(bottom=0.2)
# 設(shè)置X軸刻度為日期時間
ax.xaxis_date()
# X軸刻度文字傾斜45度
plt.xticks(rotation=45)
plt.title("股票代碼:601558兩年K線圖")
plt.xlabel("時間")
plt.ylabel("股價(元)")
mpf.candlestick_ohlc(ax,quotes,width=1.2,colorup='r',colordown='green')
plt.grid(True)

          

繪制出來的K線圖如下:

利用python numpy+matplotlib繪制股票k線圖的方法_第2張圖片

2.股票數(shù)據(jù)來源于Tushare:

因為從Tushare中獲取到的數(shù)據(jù)為Pandas的DataFrame結(jié)構(gòu),需要將其轉(zhuǎn)換為matplotlib.finance.candlestick_ohlc()方法能夠處理的數(shù)據(jù)結(jié)構(gòu)。

            
from matplotlib.pylab import date2num
import datetime
 
# 對tushare獲取到的數(shù)據(jù)轉(zhuǎn)換成candlestick_ohlc()方法可讀取的格式
data_list = []
for dates,row in hist_data.iterrows():
  # 將時間轉(zhuǎn)換為數(shù)字
  date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
  t = date2num(date_time)
  open,high,low,close = row[:4]
  datas = (t,open,high,low,close)
  data_list.append(datas)
 
# 創(chuàng)建子圖
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
# 設(shè)置X軸刻度為日期時間
ax.xaxis_date()
plt.xticks(rotation=45)
plt.yticks()
plt.title("股票代碼:601558兩年K線圖")
plt.xlabel("時間")
plt.ylabel("股價(元)")
mpf.candlestick_ohlc(ax,data_list,width=1.5,colorup='r',colordown='green')
plt.grid()

          

同樣也能繪制會一樣的K線圖:

利用python numpy+matplotlib繪制股票k線圖的方法_第3張圖片

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 邓州市| 大名县| 正定县| 栾川县| 邯郸市| 阿巴嘎旗| 新化县| 德阳市| 东兴市| 天等县| 封开县| 扬州市| 太原市| 子洲县| 武陟县| 阜平县| 平塘县| 鸡西市| 宁陕县| 西乌珠穆沁旗| 五家渠市| 长沙县| 墨玉县| 四子王旗| 蒙阴县| 海口市| 周口市| 达日县| 北川| 大石桥市| 鞍山市| 静海县| 苏州市| 齐河县| 准格尔旗| 晋中市| 托里县| 贵州省| 玛多县| 宁波市| 昌吉市|