Black Lives Matter. Please consider donating to Black Girls Code today.

Replacing an empty graph with a message

If a user query results in no data, instead of showing the following empty set of axes as pasted below, I’d like to show an error message.

Currently the callback is something like app.callback(Output('tofu-chart', 'figure'), [Input('page-state', 'children')].

Is the only way to do this to make the output of my callback be a Div into which I insert a figure myself?

If you want to keep the dcc.Graph around, you could do this with layout.annotations:

return {
    "layout": {
        "xaxis": {
            "visible": false
        },
        "yaxis": {
            "visible": false
        },
        "annotations": [
            {
                "text": "No matching data found",
                "xref": "paper",
                "yref": "paper",
                "showarrow": false,
                "font": {
                    "size": 28
                }
            }
        ]
    }
}

Makes this graph:

HI Alex. How can add this annotatios in python?

Example:

if data:
fig.add_trace(go.Scatter(x=fechaDoas, y=d1, marker_color=‘white’),
row=7, col=1)
else:
showanotation in this subplot

Thank you in advance

Check out https://plot.ly/python/text-and-annotations/#simple-annotation

To get the annotation into that specific subplot you’ll need to either use xref and yref to match the x/y axis ids you have at row=7, col=1 (if you want the annotation positioned in data coordinates) or xref='paper', yref='paper' and find the x/y locations (as fraction of the plot area) matching that location in the plot. I actually don’t know if we have any facilities in the Python library to help find those references or locations based on row and col - @nicolaskruchten?

add_annotation() supports row and col… does that meet your needs @alexcjohnson ?

Ah nice, I wasn’t able to find that in the docs but that’s great! Does that just set xref/yref to the subplot at that location (for data-referenced annotations), or would it also do something useful for positioning paper-referenced annotations within that grid cell?

row/col is used to target xref/yref automatically (for figures created with make_subplots only! including px). It’s not as good at targeting paper coordinates just yet IIRC.