bokeh 그래프 티스토리에 embedding 하기!
본문 바로가기
기타 저장소/유용한 tool

bokeh 그래프 티스토리에 embedding 하기!

by _HEBB 2020. 3. 8.

티스토리에 bokeh 임베딩하기



2020/02/20 - [기타 저장소/유용한 tool] - 파이썬 시각화의 끝판왕 bokeh 사용하기


bokeh 에 대한 이전글에서 중요한 한가지를 빼먹었는데, 블로그에 그래프를 임베딩하는 것이었다. 당시에는 할 줄 몰라서 안했는데, 꽤 간단한 것이었다. 그래서 업데이트하려고 했는데, 문제는 티스토리 에디터 신버전에선 불가능한 것 같다. 할 수 없이 구버전 글쓰기로 쓴다.


일단 주의사항

  1. 구버전 글쓰기로 HTML 코드 삽입해야함
  2. 그래프가 반응형은 아닌듯. 애초에 만들때 적당한 사이즈로 만들어야함.
  3. 그래츠의 사이즈가 가로로 너무 크면 사이드바를 겹치거나 가릴 수도 있다.
  4. 모바일에서도 잘 표시되지만, 모바일에선 몇가지 기능들이 작동안함.

등이 있는데 나머지는 문제가 없다. 아래 코드를 그냥 복붙하면 html 파일이 만들어진다.


import pandas as pd


# 데이터

data = [['20190219', 125000.0, 127500.0, 123500.0, 126000.0, 57757], ['20190220', 125000.0, 127000.0, 124500.0, 126000.0, 68453], ['20190221', 125500.0, 126000.0, 124000.0, 125000.0, 43961], ['20190222', 125000.0, 125000.0, 123500.0, 125000.0, 31065], ['20190225', 125500.0, 126000.0, 124000.0, 125500.0, 45852], ['20190226', 125000.0, 127000.0, 124500.0, 126500.0, 37404], ['20190227', 126500.0, 127000.0, 124500.0, 126000.0, 36131], ['20190228', 126500.0, 127000.0, 124500.0, 125000.0, 69474], ['20190304', 124500.0, 125500.0, 122500.0, 123500.0, 65517], ['20190305', 123000.0, 125000.0, 122000.0, 124500.0, 35186], ['20190306', 124000.0, 124500.0, 122500.0, 123500.0, 35449], ['20190307', 123500.0, 124000.0, 122000.0, 123500.0, 34768], ['20190308', 122500.0, 122500.0, 120500.0, 121500.0, 35118], ['20190311', 121500.0, 122500.0, 120000.0, 122500.0, 39576], ['20190312', 123000.0, 124000.0, 122000.0, 123500.0, 24117], ['20190313', 123000.0, 123500.0, 121500.0, 123500.0, 37649], ['20190314', 123000.0, 124000.0, 122000.0, 123500.0, 95132], ['20190315', 123000.0, 128000.0, 123000.0, 126500.0, 107246], ['20190318', 127000.0, 131000.0, 126500.0, 131000.0, 74644], ['20190319', 130000.0, 134000.0, 129500.0, 133000.0, 68348], ['20190320', 132000.0, 133000.0, 129500.0, 131000.0, 42697], ['20190321', 130000.0, 132000.0, 127500.0, 128500.0, 54018], ['20190322', 127500.0, 129500.0, 126500.0, 127000.0, 32380], ['20190325', 125500.0, 126500.0, 124000.0, 124500.0, 37185], ['20190326', 124500.0, 125500.0, 123500.0, 124000.0, 45161], ['20190327', 124000.0, 125000.0, 123500.0, 124000.0, 34336], ['20190328', 124000.0, 124500.0, 120500.0, 121500.0, 43518], ['20190329', 122500.0, 125000.0, 122000.0, 124500.0, 39035], ['20190401', 124000.0, 126500.0, 124000.0, 126500.0, 22463], ['20190402', 125500.0, 126500.0, 123500.0, 126000.0, 31754], ['20190403', 124500.0, 128000.0, 124500.0, 128000.0, 36250], ['20190404', 128500.0, 128500.0, 126000.0, 128000.0, 34854], ['20190405', 127500.0, 129000.0, 126000.0, 127500.0, 33513], ['20190408', 127500.0, 128000.0, 126000.0, 128000.0, 39005], ['20190409', 128000.0, 129000.0, 127500.0, 128500.0, 33266], ['20190410', 128000.0, 129000.0, 126500.0, 128000.0, 64476], ['20190411', 128500.0, 129000.0, 125000.0, 125000.0, 84802], ['20190412', 126000.0, 127500.0, 125500.0, 127000.0, 39663], ['20190415', 126500.0, 128500.0, 126000.0, 127000.0, 61140], ['20190416', 127000.0, 129000.0, 126500.0, 128500.0, 40123], ['20190417', 128500.0, 129000.0, 127000.0, 128000.0, 30846], ['20190418', 128000.0, 128500.0, 124000.0, 124500.0, 55346], ['20190419', 124500.0, 125000.0, 122000.0, 123000.0, 53439], ['20190422', 123000.0, 123500.0, 121000.0, 122500.0, 30421], ['20190423', 122500.0, 123500.0, 120500.0, 121500.0, 54997], ['20190424', 122500.0, 122500.0, 119500.0, 120000.0, 63486], ['20190425', 121000.0, 121000.0, 118500.0, 119000.0, 36046], ['20190426', 118500.0, 119500.0, 117000.0, 119000.0, 43749], ['20190429', 119000.0, 119500.0, 117000.0, 119500.0, 33516], ['20190430', 122000.0, 123000.0, 119000.0, 119500.0, 94118], ['20190502', 118500.0, 122000.0, 118000.0, 121000.0, 56723], ['20190503', 121000.0, 122000.0, 119500.0, 120000.0, 35240], ['20190507', 118500.0, 119500.0, 117000.0, 117500.0, 44453], ['20190508', 116500.0, 117000.0, 115000.0, 116500.0, 52805], ['20190509', 117000.0, 117000.0, 113000.0, 113000.0, 116012], ['20190510', 113000.0, 114500.0, 110000.0, 111500.0, 86072], ['20190513', 110500.0, 110500.0, 107500.0, 108500.0, 70847], ['20190514', 107500.0, 108000.0, 105000.0, 106500.0, 92820], ['20190515', 107000.0, 107500.0, 105000.0, 107000.0, 68937], ['20190516', 107000.0, 107500.0, 104000.0, 105000.0, 64047]]


df = pd.DataFrame(data, columns=['date', 'open', 'high', 'low', 'close', 'volume'])


from bokeh.plotting import figure, show, output_file

from bokeh.layouts import gridplot


# 파일 생성

output_file('plot.html')


inc = df.close >= df.open

dec = df.open > df.close


p_candlechart = figure(plot_width=800, plot_height=200, x_range=(-1, len(df)), tools=['xpan, crosshair, xwheel_zoom, reset, hover, box_select, save'])

p_candlechart.segment(df.index[inc], df.high[inc], df.index[inc], df.low[inc], color="red")

p_candlechart.segment(df.index[dec], df.high[dec], df.index[dec], df.low[dec], color="blue")

p_candlechart.vbar(df.index[inc], 0.5, df.open[inc], df.close[inc], fill_color="red", line_color="red")

p_candlechart.vbar(df.index[dec], 0.5, df.open[dec], df.close[dec], fill_color="blue", line_color="blue")


p_volumechart = figure(plot_width=800, plot_height=100, x_range=p_candlechart.x_range, tools="crosshair")

p_volumechart.vbar(df.index, 0.5, df.volume, fill_color="black", line_color="black")


p = gridplot([[p_candlechart], [p_volumechart]])

show(p)


구버전에서는 코드블럭을 어떻게 넣는지 몰라서 대충 넣었다. 아마 gist 로 넣을 수는 있을건데 다음 포스팅때부터 그렇게하고 지금은 일단 임베딩만을 위해서 간단히 한다. 만들어진 'plot.html' 파일의 텍스트를 복사해서, 티스토리 구버전 에디터에서 HTML 체크한 뒤, 붙여넣는다. 미리보기를 하면 이쁘게 차트가 그려지는 것을 볼 수 있다.


이렇게



Bokeh Plot



상단의 툴박스를 사용하면 다양한 기능을 쓸 수 있다. 이미지를 저장할수도 있고, 마우스 휠로 움직일수도 있다. 차트를 잡고 좌우로 끌어당길수도 있다. 물론 더 많은 툴을 적용할 수 있고, on/off 할 수 있다.


댓글