密谋:尝试绘制次要Y轴会引发错误

时间:2020-11-10 04:15:34

标签: python plotly plotly-dash

我正在尝试绘制次Y轴,并且通常遵循fig = go.Figure{data = [], layout = []}。我在条形图上添加了一条散点线。使用相同的格式,我可以绘制堆积的条形图,但是不能绘制散点图。看来我需要先创建子图才能用作辅助轴?

这是我收到的例外。

跟踪:

Exception: In order to reference traces by row and column, you must first use plotly.tools.make_subplots to create the figure with a subplot grid.

## Traceback *(most recent call last)*

"/Users/packages/plotly/basedatatypes.py", line *1856* , in `_validate_get_grid_ref`

raise AttributeError("_grid_ref")

* During handling of the above exception, another exception occurred:

代码:

# Velocity graph
@app.callback(Output("velocity-graph", "figure"),
              [
                 Input("submarket-select", "value")
             
              ],
             )
def update_velocity(submarket):

    if submarket:

        df = df_read

        #df = df.loc[df["Year"] >= 2010]

        fig = go.Figure(

            data=[

                  go.Bar(
                        x=df["Date_Col"],
                        y=df["NetAbsorptionUnits"],
                        name="Net Absorption"
                        ),

                  ],

            layout=go.Layout(

                title="Market Velocity : Rent Growth, Vacancy and Net Absorption",

                xaxis=dict(
                    tickangle=60,
                    tickfont=dict(family="Rockwell", color="crimson", size=14)
                ),

                yaxis=dict(
                    title="Net Absorption Rate",
                    showticklabels=True
                ),

                barmode="stack",

            )
        )

        fig.add_trace(go.Scatter(
                        x=df["Date_Col"],
                        y=df["VacantPct"],
                        name="Vacancy Rate",
                        marker=dict(color="rgb(204,0,0)")
                        ),
                        secondary_y=False,
        )

        fig.add_trace(go.Scatter(
                        x=df["Date_Col"],
                        y=df["AskingPctChg"],
                        name="Rent Growth",
                        marker=dict(color="rgb(0,153,76)")
                        ),
                        secondary_y=True,
        )

        fig.update_layout(template=plotly_template)

        return fig

    else:

        return (no_update)

0 个答案:

没有答案