英文:
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。
我之前通过编写一堆True
和False
来实现,但数据框是动态的,所以静态解决方案不会起作用(它可以有更多行,也可以是乱序的等等)。
提前感谢!
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论