Plot multiple columns on line graph using Dash/Plotly

被刻印的时光 ゝ 提交于 2019-12-13 08:36:14

问题


I have a table that has looks like this ...

ticker,price1y,price2y,price3y,price5y,
aapl,12,23,47,69,
tsla,-9,24,54,190,
att,-10,23,34,35,

I would like to plot these using pandas plotly in dash to show price1y price2y ... price5y along the x axis and % change up the y axis. i need to be able to select multiple values to add to the graph using dash's callback feature.

i currently create a dash_core_components graph however i have been unsuccessfully in plotting to this.

app.layout = html.Div([
        html.Div([
                     dcc.Graph(
                        id='bar-graph'

                        )
            ], className='twelve columns')

Thanks,


回答1:


You can use grouped bar chart from plotly:

Do the imports:

import plotly.plotly as py
import plotly.graph_objs as go

Example dataframe:

data = {
    'ticker': ['aapl', 'tsla', 'att'],
    'price1y': [12 ,-9 ,-10],
    'price2y': [23 ,24 ,23],
    'price3y': [47 ,54 ,34],
    'price5y': [69 ,190 ,35]
}
df = pd.DataFrame(data).set_index('ticker')

Looks like:

        price1y price2y price3y price5y
ticker              
aapl      12    23        47    69
tsla      -9    24        54    190
att      -10    23        34    35

Then you can iterate over the columns and dynamically create the data for the grouped bar chart:

res = []
for col in df.columns:
    res.append(
        go.Bar(
            x=df.index.values.tolist(),
            y=df[col].values.tolist(),
            name=col
        )
    )

layout = go.Layout(
    barmode='group'
)

fig = go.Figure(data=res, layout=layout)
py.iplot(fig, filename='grouped-bar')

This will produce:

In order to get that in Dash, you'll need to return the above from a callback.



来源:https://stackoverflow.com/questions/55809312/plot-multiple-columns-on-line-graph-using-dash-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!