Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all traces from plotly.js plot

I have a page containing a plot.ly plot, that I want to write data to a few times, overwriting what was there before.

I can't find a way to remove all traces from my plotly.js plot, and just replotting adds the data without removing the old.

like image 226
Ian Avatar asked Aug 12 '15 21:08

Ian


People also ask

How do I remove a trace name in Plotly?

If you want to hide the trace names in a box chart, you could hide the axis' labels by using showticklabels = F . In the example below the trace name is also hidden in the hover labels by setting hoverinfo = 'x' .

What does Add_trace do in Plotly?

Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure. This allows you to start with an empty figure, and add traces to it sequentially.

What are traces in Plotly?

A plotly. graph_objects. Image trace is a graph object in the figure's data list with any of the named arguments or attributes listed below. Display an image, i.e. data on a 2D regular raster. By default, when an image is displayed in a subplot, its y axis will be reversed (ie.

Is Plotly express the same as Plotly?

Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures. Every Plotly Express function uses graph objects internally and returns a plotly.graph_objects.Figure instance.


2 Answers

There's two ways of doing this, both listing in the plotlyjs function reference page.

Option 1 (tell plotly to delete the trace(s) in question):

Plotly.deleteTraces(graphDiv, 0);

where the second argument is the trace index of the trace to delete. Note that this second argument can also be an array of indices allowing you to delete multiple traces at once.

Option 2 (tell plotly to make a new plot with new data):

 Plotly.newPlot(graphDiv, data, layout);

where the arguments are the same as for Plotly.plot. This creates a new plot drawing only the data sent in the second argument. More precisely, Plotly.newPlot is idempotent whereas Plotly.plot isn't.

like image 186
etpinard Avatar answered Oct 02 '22 05:10

etpinard


you can remove the last trace, and the previous one, and so one until the last one :

while(graphDiv.data.length>0)
{
      Plotly.deleteTraces(graphDiv, [0]);
}
like image 44
Pierre GUILLAUME Avatar answered Oct 02 '22 04:10

Pierre GUILLAUME