# -*- coding: utf-8 -*- # ohlc_chart_maker.py # ========================== # * Version : 0.1 # * Last update : 2019-01-24 # import numpy import mpl_finance from matplotlib import dates from matplotlib import pyplot from datetime import datetime as DateTime def make_mpl_dates(year, month, day): """ @return Matplotlib dates """ return dates.date2num(DateTime(year, month, day)) def get_test_data(): """ @return test 2d array (Time, Open, High, Low, Close, Volume) """ data = numpy.array([ [make_mpl_dates(2019, 1, 14), 90, 105, 85, 100, 1000], [make_mpl_dates(2019, 1, 15), 110, 110, 110, 110, 1100], [make_mpl_dates(2019, 1, 16), 125, 125, 115, 120, 1200], [make_mpl_dates(2019, 1, 17), 120, 140, 120, 140, 1300], [make_mpl_dates(2019, 1, 18), 130, 145, 130, 135, 1200], [make_mpl_dates(2019, 1, 21), 125, 125, 110, 110, 1100], [make_mpl_dates(2019, 1, 22), 115, 120, 90, 105, 900], [make_mpl_dates(2019, 1, 23), 110, 115, 100, 110, 1100], [make_mpl_dates(2019, 1, 24), 130, 135, 125, 125, 1400], [make_mpl_dates(2019, 1, 25), 120, 140, 115, 135, 1500]]) return data if __name__ == "__main__": # Time, Open, High, Low, Close, Volume data = get_test_data() fig = pyplot.figure() ax1 = fig.add_subplot(1, 1, 1) ax2 = ax1.twinx() # 「OHLC チャート」と「出来高」の表示比 (2 なら 2:1 で表示する) pv_yratio = 2 # ax1 settings price_max = 145 price_min = 85 price_ymax = price_max + 5 price_ymin = price_min - (price_ymax - price_min) / pv_yratio ax1.set_ylim(price_ymin, price_ymax) mpl_finance.plot_day_summary_ohlc(ax1, data, ticksize=8) # ax2 settings volume_max = 1500 volume_ymax = volume_max * (pv_yratio + 1) ax2.set_ylim(0, volume_ymax) ax2.bar(data[:, 0], data[:, 5], width=0.25) pyplot.show()