Jupyter에서 Plotly로 Bargraph Button 구현하기

2019. 5. 12. 23:05분석 Python/Visualization

728x90

Jupyter에서 widget으로도 그래프에서 버튼 기능이 구현이 되는 것으로 알고 있지만,

Plotly를 다른 데에서 버튼 기능을 유지한 채로 적용해야 할 일이 있어서, 구현을 연습해봅니다.

 

widget도 언제 가는 해야겠죠?....

https://lineup.js.org/integrations/jupyter.html

http://hselab.org/jupyter-widgets-explore-queueing-models.html

 

 

아무튼 지금은 https://data-newbie.tistory.com/106 보여준 Lasso coef 결과물을 Plotting 해보려고 합니다.

 

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

data = [
    go.Bar(
        x = list(rang) , 
        y = reg.coef_  ,
        marker = dict(
          color='rgba(55, 128, 191, 0.7)',
        ),
        name = 'Cross Validatiaon Coef'
    ),
    go.Bar(
        x = list(rang),
        y = coef ,
        base = 0,
        marker = dict(
          color='rgba(219, 64, 82, 0.7)',
        ),
        name = 'Gradient Desecnt Coef'
    )
]

updatemenus = list([
    dict(type="buttons",
         active=-1,
         buttons=list([
            dict(label = 'GradientDescent',
                 method = 'update',
                 args = [{'visible': [True,False]},
                         {'title': 'Coordinate Descent Coef'}]),
            dict(label = 'CrossValidation',
                 method = 'update',
                 args = [{'visible': [False, True]},
                         {'title': 'CrossValidation Coef'}]),
            dict(label = 'Reset',
                 method = 'update',
                 args = [{'visible': [True,  True]},
                         {'title': 'Both'}])
        ]),
    )
])

layout = dict(title='Coefficeint [CrossValidation and GradientDescent]', showlegend=True,
              updatemenus=updatemenus)


fig = go.Figure(data=data, layout=layout)

## offline notebook 용으로 iplot
iplot(fig, filename='base-bar')

 

버튼을 누르면 보이지만 저렇게 됩니다!

 

 

 

https://towardsdatascience.com/interactive-visualization-with-dash-and-plotly-29eaccc90104

https://plot.ly/python/custom-buttons/

728x90