我想要一个Plotly下拉菜单来根据列的唯一值来筛选数据框。

huangapple go评论66阅读模式
英文:

I want a plotly dropdown to filter a dataframe by a column unique values

问题

Good evening!

我有一个类似这样的数据框:

df = pd.DataFrame({
    'distance': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'Speed': [1, 2, 3, 1.2, 2.2, 3.2, 1.3, 2.3, 3.3],
    'Lap': ['Lap 1', 'Lap 1', 'Lap 1', 'Lap 2', 'Lap 2', 'Lap 2', 'Lap 3', 'Lap 3', 'Lap 3']
})

我想使用plotly绘制它,使用一个下拉菜单来过滤绘制的线,根据列"lap"的唯一值 ('Lap 1', 'Lap 2', 'Lap 3')。

注意:由于Flask的限制,我无法使用Dash。

我之前通过编写一堆TrueFalse来实现,但数据框是动态的,所以静态解决方案不会起作用(它可以有更多行,也可以是乱序的等等)。

提前感谢!

英文:

Good evening!

I have a dataframe like this one:

df = pd.DataFrame({
    'distance': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'Speed': [1, 2, 3, 1.2, 2.2, 3.2, 1.3, 2.3, 3.3],
    'Lap': ['Lap 1', 'Lap 1', 'Lap 1', 'Lap 2', 'Lap 2', 'Lap 2', 'Lap 3', 'Lap 3', 'Lap 3']
})

I want to plot it using plotly, using a dropdown filtering the lines plotted, by the column "lap" unique values ( 'Lap 1', 'Lap 2', 'Lap 3').

Note: I cannot use Dash due to Flask restrictions.

I was able to do so by writing a bunch of Trues and Falses but the dataframe is dynamic so a static solution is not gonna work (it can have more rows, it can be scrambled etc)

Thanks in advance!

答案1

得分: 0

以下是您要翻译的代码部分:

import plotly.graph_objects as go
import numpy as np
import pandas as pd

df = pd.DataFrame({
    'distance': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'Speed': [1, 2, 3, 1.2, 2.2, 3.2, 1.3, 2.3, 3.3],
    'Lap': ['Lap 1', 'Lap 1', 'Lap 1', 'Lap 2', 'Lap 2', 'Lap 2', 'Lap 3', 'Lap 3', 'Lap 3']
})

data = []
for lap in df['Lap'].unique():
    data.append(
        go.Scatter(
            x=df[df['Lap']==lap]['distance'],
            y=df[df['Lap']==lap]['Speed'],
            name=lap
        )
    )

layout = go.Layout(
    title='Speed vs Distance by Lap',
    xaxis=dict(title='Distance'),
    yaxis=dict(title='Speed')
)

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

fig.update_layout(
    updatemenus=[
        dict(
            buttons=[
                dict(
                    label=lap,
                    method="update",
                    args=[{"visible": [lap == trace.name for trace in fig.data]},
                          {"title": "Speed vs Distance for " + lap}]
                ) for lap in df['Lap'].unique()
            ],
            direction="down",
            showactive=True,
            x=1,
            xanchor="right",
            y=1.2,
            yanchor="top"
        )
    ]
)

fig.show()

希望这对您有所帮助。

英文:

You can simply do this:

import plotly.graph_objects as go
import numpy as np
import pandas as pd


df = pd.DataFrame({
    'distance': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'Speed': [1, 2, 3, 1.2, 2.2, 3.2, 1.3, 2.3, 3.3],
    'Lap': ['Lap 1', 'Lap 1', 'Lap 1', 'Lap 2', 'Lap 2', 'Lap 2', 'Lap 3', 'Lap 3', 'Lap 3']
})

data = []
for lap in df['Lap'].unique():
    data.append(
        go.Scatter(
            x=df[df['Lap']==lap]['distance'],
            y=df[df['Lap']==lap]['Speed'],
            name=lap
        )
    )

layout = go.Layout(
    title='Speed vs Distance by Lap',
    xaxis=dict(title='Distance'),
    yaxis=dict(title='Speed')
)

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

fig.update_layout(
    updatemenus=[
        dict(
            buttons=[
                dict(
                    label=lap,
                    method="update",
                    args=[{"visible": [lap == trace.name for trace in fig.data]},
                          {"title": "Speed vs Distance for " + lap}]
                ) for lap in df['Lap'].unique()
            ],
            direction="down",
            showactive=True,
            x=1,
            xanchor="right",
            y=1.2,
            yanchor="top"
        )
    ]
)

fig.show()

我想要一个Plotly下拉菜单来根据列的唯一值来筛选数据框。

huangapple
  • 本文由 发表于 2023年4月17日 06:13:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定