Remove all traces

Hello! I’m really enjoying the new Plotly Python library, it’s great.

I don’t know if this is a daft question but I wonder if there is a function to delete all traces from my chart, to revert it to an empty FigureWidget?

I’m setting up a scatterplot with several different datasets that I would like to compare. I start with an empty FigureWidget and add scatter traces using ‘fig.add_scatter’ using a dropdown list to choose which of my datasets I’d like to load to compare.

What I’d really like to be able to do is add a button which removes all my added traces and returns it to an empty plot so that a user can reset the chart and start over choosing traces to compare, but I can’t work out how to do this.

I’ve tried just making a button that runs go.FigureWidget() again but this doesn’t do anything, my chart looks the same!

Any ideas?



Hi @KidSampson, glad you’re enjoying the library!

You can remove all traces for a FigureWidget by assigning an empty list the the data property = []

You can also remove only select traces by assigning the data property to a subset of itself. So to remove the second trace, but keep the first and third trace: = [[0],[2]]

If you need to clear the layout as well, assign an emtpy dict to the layout property

fig.layout = {}

Hope that helps!


That’s brilliant, I thought it had to be something like that but I couldn’t work it out. Thank you!

1 Like

Thanks @jmmease, That was a simple but a brilliant solution.
I was using three dropdowns to plot traces (corresponding to the selected dropdown values) in a graph object. When the dropdown values are cleared i.e when no values are selected, I wanted the graph object to remove all traces but retain the layout properties (x-axis, yaxis range, ticks etc.).

Various solutions I had tried before either removed the Graph object in its entirety or placed an empty graph object. The layout would also disappear. Aesthetics wise this was an eyesore.

So, I came across your solution. Within the callback, I initialised one graph object with valid data and the desired layout. In the next step, I removed all the traces while retaining the layout. = []

I will further look for ways to finetune it further.
But thanks for the solution.