Plotly 将空白图显示为具有所需网格(x,y,facet_row)但没有数据的输出,而它显示其他数据集的图(或图)

Plotly is showing blank graph as output with required grid (x, y, facet_row) but without data whereas it is showing fig (or graph) for other datasets

我正在尝试绘制数据框:dataframe which has 5 columns, out of which, x axis should be week, geogname should be dropdown list, trace should be used as color and dataframe is facet_row, after plotting I should get graph something like: expected graph for every facet_row, but I have been getting something like: Current_Output_graph,我做错了什么?

关于dataset,有将近500000行,trace有5个相同的值,并按顺序排列,dataframe共有16种类型的值,例如:其中1个是CAT。

fig = px.line(
    df,
    x="Week",
    y="value",
    color="trace",
    facet_row="dataframe",
)

# default state...
fig.for_each_trace(lambda t: t.update(visible=(t.name[0:2] == "NY")))

fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {"label": state, "method": "restyle", "args": [{"visible":[t.name[0:2]==state for t in fig.data]}]}
                for state in df["GeogName"].unique()
            ],
            "y":1.01,
            "x":.5
        }
    ],
    autosize=False,
    #height=1500
    height=9000,
    width=1000
)

# compress up space between subplots
fig.update_layout({
    f"yaxis{'' if axis==0 else axis+1}": {"domain": [s, e-.002]}
    for axis, (s, e) in enumerate(
        zip(np.linspace(0, 1, len(d.keys())+1), np.linspace(0, 1, len(d.keys())+1)[1:])
    )
})
  • 当你整合数据框时,你没有连接 GeogNamecolumn name
  • 添加一行代码使其工作
df["trace"] = df["GeogName"]+df["trace"]
  • 还修改了代码以在子图之间压缩 space,因此它不依赖于 dict

带有下载数据的完整代码

from pathlib import Path
import pandas as pd
import plotly.express as px

df = pd.read_csv(Path.home().joinpath("Downloads/data.csv"), index_col=0)

### trace has to contain GeogName so it is first two characters in trace name 
df["trace"] = df["GeogName"]+df["trace"]

fig = px.line(
    df,
    x="Week",
    y="value",
    color="trace",
    facet_row="dataframe",
)


# default state...
fig.for_each_trace(lambda t: t.update(visible=(t.name[0:2] == "NY")))

fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {"label": state, "method": "restyle", "args": [{"visible":[t.name[0:2]==state for t in fig.data]}]}
                for state in df["GeogName"].unique()
            ],
            "y":1.01,
            "x":.5
        }
    ],
    autosize=False,
    #height=1500
    height=9000,
    width=1000
)

# compress up space between subplots
fig.update_layout({
    f"yaxis{'' if axis==0 else axis+1}": {"domain": [s, e-.002]}
    for axis, (s, e) in enumerate(
        zip(np.linspace(0, 1, len(df["dataframe"].unique())+1), np.linspace(0, 1, len(df["dataframe"].unique())+1)[1:])
    )
})