使用Dash/Plotly在折线图上绘制多列 [英] Plot multiple columns on line graph using Dash/Plotly

查看:880
本文介绍了使用Dash/Plotly在折线图上绘制多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张看起来像这样的桌子...

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,

我想用熊猫将它们绘制成虚线,以在x轴上显示price1y price2y ... price5y,并且在y轴上%改变.我需要能够使用破折号的回调功能选择多个值添加到图形中.

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.

我目前正在创建dash_core_components图,但是在绘制该图时一直没有成功.

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')

谢谢

推荐答案

您可以使用分组条形图从情节:

执行导入:

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

示例数据框:

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')

看起来像:

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

然后,您可以遍历列动态创建分组条形图的数据:

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')

这将产生:

为了在Dash中获得它,您需要从回调中返回上述内容.

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

这篇关于使用Dash/Plotly在折线图上绘制多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆