Plotly python:多项选择交互式绘图
Posted
技术标签:
【中文标题】Plotly python:多项选择交互式绘图【英文标题】:Plotly python: Multiple item selection interactive plot 【发布时间】:2021-10-24 19:02:39 【问题描述】:我希望能够选择多个项目,而不是仅选择一项或全部。到目前为止,我只能选择一项并显示其图表,或者选择“全部” 并显示所有图表。
我希望能够选择 2 个或更多项目并将它们的图显示在如下所示的单个图表中。
我是 plotly 的新手,因此非常感谢任何帮助。非常感谢:)
我的代码:
def interactive_multi_plot(actual, forecast_1, forecast_2, title, addAll = True):
fig = go.Figure()
for column in forecast_1.columns.to_list():
fig.add_trace(
go.Scatter(
x = forecast_1.index,
y = forecast_1[column],
name = "Forecast_SI"
)
)
button_all = dict(label = 'All',
method = 'update',
args = ['visible': forecast_1.columns.isin(forecast_1.columns),
'title': 'All',
'showlegend':True])
for column in forecast_2.columns.to_list():
fig.add_trace(
go.Scatter(
x = forecast_2.index,
y = forecast_2[column],
name = "Forecast_LSTM"
)
)
button_all = dict(label = 'All',
method = 'update',
args = ['visible': forecast_2.columns.isin(forecast_2.columns),
'title': 'All',
'showlegend':True])
for column in actual.columns.to_list():
fig.add_trace(
go.Scatter(
x = actual.index,
y = actual[column],
name = "True values"
)
)
button_all = dict(label = 'All',
method = 'update',
args = ['visible': actual.columns.isin(actual.columns),
'title': 'All',
'showlegend':True])
fig.layout.plot_bgcolor = '#010028'
fig.layout.paper_bgcolor = '#010028'
def create_layout_button(column):
return dict(label = column,
method = 'update',
args = ['visible': actual.columns.isin([column]),
'title': column,
'showlegend': True])
fig.update_layout(
updatemenus=[go.layout.Updatemenu(
active = 0,
buttons = ([button_all] * addAll) + list(actual.columns.map(lambda column: create_layout_button(column)))
)
]
)
# Update remaining layout properties
fig.update_layout(
title_text=title,
height=800,
font = dict(color='#fff', size=12)
)
fig.show()
【问题讨论】:
【参考方案1】:这在 dash 中很简单,因为 https://dash.plotly.com/dash-core-components/dropdown 支持 多选和搜索
设置-
对您的功能进行了微小的更改。定义跟踪的 meta 属性以供以后在 dash 回调中使用
模拟一些未提供的数据。三个数据框(实际、预测 1 和预测 2)
使用修改后的代码创建图形
设置
import pandas as pd
import numpy as np
import plotly.graph_objects as go
def interactive_multi_plot(actual, forecast_1, forecast_2, title, addAll = True):
fig = go.Figure()
for column in forecast_1.columns.to_list():
fig.add_trace(
go.Scatter(x = forecast_1.index,y = forecast_1[column],name = "Forecast_SI",meta = column))
for column in forecast_2.columns.to_list():
fig.add_trace(
go.Scatter(x = forecast_2.index,y = forecast_2[column],name = "Forecast_LSTM" ,meta = column))
for column in actual.columns.to_list():
fig.add_trace(
go.Scatter(x = actual.index,y = actual[column],name = "True values" ,meta = column))
fig.layout.plot_bgcolor = '#010028'
fig.layout.paper_bgcolor = '#010028'
# Update remaining layout properties
fig.update_layout(
title_text=title,
height=800,
font = dict(color='#fff', size=12)
)
return fig
S = 100
C = 10
actual = pd.DataFrame(
c: np.sort(np.random.uniform(0, 600, S))
for c in [f"ab-c" for a, b, c in zip(np.random.randint(100, 200, C),
np.random.choice(list("ABCDEF"), C),
np.random.randint(300, 400, C),)]
)
f1 = actual.assign(**c:actual[c]*1.1 for c in actual.columns)
f2 = actual.assign(**c:actual[c]*1.2 for c in actual.columns)
fig = interactive_multi_plot(actual, f1, f2, "Orders")
dash 应用
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from jupyter_dash import JupyterDash
# Build App
app = JupyterDash(__name__)
app.layout = html.Div(
[
dcc.Dropdown(
id="lines",
options=["label": c, "value": c for c in actual.columns.tolist()],
value=None,
multi=True
),
dcc.Graph(id="interactive-multiplot", figure=fig),
]
)
@app.callback(
Output("interactive-multiplot", "figure"),
Input("lines", "value"),
State("interactive-multiplot", "figure"),
)
def updateGraphCB(lines, fig):
# filter traces...
fig = go.Figure(fig).update_traces(visible=False)
for line in [] if not lines else lines:
fig.update_traces(visible=True, selector="meta":line)
return fig
app.run_server(mode="inline")
【讨论】:
你好罗!你能解释一下你在代码的设置部分做什么吗?提前谢谢! 您好!谢谢您的帮助!我不明白为什么现在这些线是线性的,而且图表看起来与原始图表完全不同。另外,为什么项目名称不同,为什么随机选择?谢谢! 随机的唯一用途是生成数据帧,与绘图无关。你没有提供样本数据,所以我模拟了它,所以如果你想让我调查,我可以拨打这个电话fig = interactive_multi_plot(actual, f1, f2, "Orders")
最简单的方法是提供所有三个数据帧的样本数据,所以我不必假设它可能或可能如何不是结构化的以上是关于Plotly python:多项选择交互式绘图的主要内容,如果未能解决你的问题,请参考以下文章
使用 plotly 或 Network 3D 将 igraph 转换为交互式绘图