# Plotly dash: return a simple scatter plot in a callback function

**URL:** <https://community.plotly.com/t/plotly-dash-return-a-simple-scatter-plot-in-a-callback-function/66567>\
**Category:** Dash Python\
**Created:** [July 30, 2022, 4:34pm UTC](https://community.plotly.com/t/plotly-dash-return-a-simple-scatter-plot-in-a-callback-function/66567 "2022-07-30T16:34:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryan1](https://avatars.discourse-cdn.com/v4/letter/b/a9a28c/32.png) [@bryan1](https://community.plotly.com/u/bryan1)\
**Post date:** [July 30, 2022, 4:34pm UTC](https://community.plotly.com/t/plotly-dash-return-a-simple-scatter-plot-in-a-callback-function/66567/1 "2022-07-30T16:34:07Z")

</div>

I’m trying to return a scatter plot in a callback function after clicking a button but I have this error: Callback error updating …graph.figure…output.children…

```auto
app = Dash( __name__ )

app.layout = html.Div([
    html.Button("Plot", id="submit", n_clicks=0),
    dcc.Graph(id='graph')
])

@app.callback(
    Output('graph', 'figure'),
    Input('submit', 'n_clicks'),
)
def update_output(n_clicks):
    return px.scatter(x=[1, 2, 3], y=[2, 6, 7])

if __name__ == ' __main__':
    app.run_server(debug=True)

```

Can anyone please help me to return a simple scatter plot into a callback?

---

<div class="post-metadata">

**Author:** ![AnnMarieW](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@AnnMarieW](https://community.plotly.com/u/AnnMarieW)\
**Post date:** [July 30, 2022, 5:09pm UTC](https://community.plotly.com/t/plotly-dash-return-a-simple-scatter-plot-in-a-callback-function/66567/2 "2022-07-30T17:09:34Z")

</div>

Hi @bryan1 and welcome to the Dash community!

I ran your code without any errors.

![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/6/6/66f85d975f6598ced7421543b0f9f570127a5ee9.png)

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [July 30, 2022, 5:47pm UTC](https://community.plotly.com/t/plotly-dash-return-a-simple-scatter-plot-in-a-callback-function/66567/3 "2022-07-30T17:47:13Z")

</div>

I think you should have an option that if n\_clicks == 0 nothing shows, and when n\_clicks \> 0, it returns figure:

```auto
from dash.exceptions import PreventUpdate
app = Dash( __name__ )
app.layout = html.Div([
    html.Button("Plot", id="submit", n_clicks=0),
    dcc.Graph(id='graph',figure={})
])

@app.callback(
    Output('graph', 'figure'),
    Input('submit', 'n_clicks'),
)
def update_output(n_clicks):
    if n_clicks == 0:
        raise PreventUpdate
    elif n_clicks > 0:
        return px.scatter(x=[1, 2, 3], y=[2, 6, 7])

if __name__ == ' __main__':
    app.run_server(debug=False,port=1212)

```
