1번째 줄: |
1번째 줄: |
| + | == 개요 == |
| + | matplotlib과 mpl_finance를 함께 사용한다. |
| [[분류:기술적 분석]] | | [[분류:기술적 분석]] |
| + | {| class="wikitable" |
| + | |+ |
| + | !과정 |
| + | !설명 |
| + | !방법 |
| + | |- |
| + | |영역 설정 |
| + | |복잡한 차트를 그려야 할 땐 subplot2grid를 이용한다. |
| + | subplot2grid의 첫번째엔 전체 몇등분 할지, 2번째는 시작할 지점, rowspan, colspan은 차지할 지점을 의미한다. |
| + | |<syntaxhighlight lang="python"> |
| + | import matplotlib.pyplot as plt |
| + | import mpl_finance |
| + | |
| + | plt.rcParams["figure.figsize"] = [11.7, 8.3] # 도화지 설정. |
| + | chart = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4) |
| + | volume_chart = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4, sharex=top_axes) |
| + | |
| + | </syntaxhighlight> |
| + | |- |
| + | |캔들차트 그리기 |
| + | |chart에 그려줌을 지정한다. res는 결과를 담고 있는 dataframe. |
| + | |<syntaxhighlight lang="python"> |
| + | mpl_finance.candlestick2_ohlc(chart, res['start'], res['high'], res['low'], res['close'], width=0.5, |
| + | colorup='r', colordown='b') |
| + | </syntaxhighlight> |
| + | |- |
| + | |거래량 그리기 |
| + | | |
| + | |<syntaxhighlight lang="python"> |
| + | #---거래량 그리기 |
| + | volume_chart.get_yaxis().get_major_formatter().set_scientific(False) # 거래량 값을 그대로 표현하기 위한 옵션 |
| + | color_fuc = lambda x: 'r' if x >= 0 else 'b' # 조건을 기입한다. x가 0보다 크면 r, 작으면 b를 반환. |
| + | color_list = list(res['volume'].diff().fillna(0).apply(color_fuc)) # diff함수로 이전 값과 비교하여 조건 반영. |
| + | volume_chart.bar(res.index, res['volume'], width=0.5, align='center', color=color_list) # color_list를 따라 색칠. |
| + | </syntaxhighlight> |
| + | |} |
| + | [[분류:Matplotlib]] |