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

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?

Thanks

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

fig.data = []

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:

fig.data = [fig.data[0], fig.data[2]]

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

fig.layout = {}

Hope that helps!
-Jon

2 Likes

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

1 Like