利用利用python numpy+matplotlib绘制股票绘制股票k线图的方法线图的方法
一、一、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进行股票分析的过程中,我们可以很容易的对K线图进行绘制,下面介绍两种情形下的K线图绘制:
1. 股票数据来源于股票数据来源于Matplotlib::