python使用matplotlib绘制k线图
生活随笔
收集整理的這篇文章主要介紹了
python使用matplotlib绘制k线图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
# -*- coding:utf-8 -*-import numpy as np
import pandas as pd
import matplotlib.pyplot as pltfrom matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc#從雅虎財經(jīng)獲取歷史行情
date1 = (2017, 1, 1)
date2 = (2017, 4, 30)
quotes = quotes_historical_yahoo_ohlc('600000.ss', date1, date2)
if len(quotes) == 0:raise SystemExit#創(chuàng)建一個子圖
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)#設置主要刻度和顯示格式
mondays = WeekdayLocator(MONDAY)
mondaysFormatter = DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_major_formatter(mondaysFormatter)#設置次要刻度和顯示格式
alldays = DayLocator()
alldaysFormatter = DateFormatter('%d')
ax.xaxis.set_minor_locator(alldays)
#ax.xaxis.set_minor_formatter(alldaysFormatter)#設置x軸為日期
ax.xaxis_date()
ax.autoscale_view()
#X軸刻度文字傾斜45度
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')candlestick_ohlc(ax, quotes, width=0.6, colorup='r', colordown='g')
ax.grid(True)
plt.title('600000')
plt.show()
總結(jié)
以上是生活随笔為你收集整理的python使用matplotlib绘制k线图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: numpy的基本使用
- 下一篇: windows平台python安装Sci