利用plotly画可交互性图

plotly画图

Posted by Chauncey on May 10, 2020

可交互性python画图

Data

data = project_helper.plot_stock(pd.read_csv('data.csv', index_col=0, parse_dates=True, squeeze=True), '{} Stock'.format(apple_ticker))
data
date
2013-07-01    53.10917319
2013-07-02    54.31224742
2013-07-03    54.61204262
2013-07-05    54.17338125
2013-07-08    53.86579916
                 ...     
2017-06-26   143.57270901
2017-06-27   141.51491885
2017-06-28   143.58255490
2017-06-29   141.46568942
2017-06-30   141.80044954
Name: AAPL, Length: 1009, dtype: float64

Function

apple_ticker = 'AAPL'
project_helper.plot_stock(data, '{} Stock'.format(apple_ticker))
# project_helper
import plotly.offline as offline_py
import plotly.graph_objs as go


def plot_stock(prices, title):
    config = generate_config()
    layout = go.Layout(title=title)

    stock_trace = _generate_stock_trace(prices)
    
    offline_py.iplot({'data': [stock_trace], 'layout': layout}, config=config)
    
def generate_config():
    return {'showLink': False, 'displayModeBar': False, 'showAxisRangeEntryBoxes': True}
  
  
def _generate_stock_trace(prices):
    return go.Scatter(
        name='Index',
        x=prices.index,
        y=prices,
        line={'color': helper.color_scheme['major_line']})