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!
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
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.
fig.data = []
I will further look for ways to finetune it further.
But thanks for the solution.