- 已模拟您的数据框
- 由此我了解到每个公司都是一组线。已创建包含所有组的图
- 最后构建一个下拉菜单,让您可以进行过滤。这也可以从图例中完成
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
c = ["Sircio", "Nippon", "Toshiba", "Budget Group", "Northern Factory", "Cobot Corp"]
df1 = pd.concat(
[
pd.DataFrame(
{
"company": np.tile(c, 8),
"month": np.repeat(
pd.date_range("1-Feb-21", freq="MS", periods=8), len(c)
),
}
),
pd.DataFrame(
np.random.randint(1, 8, [len(c) * 8, 3]),
columns=["active", "registered", "productive"],
),
],
axis=1,
)
fig = go.Figure()
for c in df1["company"].unique():
fig_tmp = px.line(
df1.loc[df1["company"].eq(c)],
x="month",
y=["registered", "active", "productive"],
markers=True,
).update_traces(legendgroup=c, legendgrouptitle_text=c)
fig.add_traces(fig_tmp.data)
fig.update_layout(fig_tmp.layout)
fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": c,
"method": "restyle",
"args": ["visible", [t.legendgroup == c for t in fig.data]],
}
for c in df1["company"].unique()
]
}
]
)