使用破折号/绘图在折线图上绘制多列

2024-09-30 20:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想用熊猫在破折号中描绘这些故事,以显示昂贵的价格。。。沿x轴的价格为5y,y轴上的百分比为。我需要能够使用dash的回调功能选择多个值添加到图表中。在

我目前创建了一个仪表板核心组件图,但我一直未能成功地绘制到这一点。在

^{pr2}$

谢谢


Tags: 功能价格attticker百分比故事dash桌子
1条回答
网友
1楼 · 发布于 2024-09-30 20:25:14

您可以使用绘图中的grouped bar chart

导入:

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

示例数据帧:

^{pr2}$

看起来像:

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

这将产生:

enter image description here

为了在Dash中得到它,您需要从回调中返回上面的值。在

相关问题 更多 >