Larry Martell
Larry Martell

Reputation: 3756

plotly: removing plot and creating a new one in the same div

I have a plotly plot and when the data changes I want to remove the plot and generate a new one. To do that I do this:

$('#heatmap2').empty()

and then I regenerate my plot. But when I do that it crashes with this:

TypeError: Cannot read property 'textContent' of null
    at Array.Mo.text (plotly-latest.min.js:12)
    at Array.c.convertToTspans (plotly-latest.min.js:31)
    at Array.Mo.call (plotly-latest.min.js:12)
    at l (plotly-latest.min.js:29)
    at Object.i.syncOrAsync (plotly-latest.min.js:30)
    at Array.r (plotly-latest.min.js:29)
    at Array.Mo.call (plotly-latest.min.js:12)
    at Object.s.draw (plotly-latest.min.js:29)
    at r (plotly-latest.min.js:28)
    at Object.w.colorbar (plotly-latest.min.js:38)

Here's a codepen that shows that after calling empty() on the div you can't create another plot in it:

http://codepen.io/anon/pen/ZbZpBE?editors=101

this doesn't seem to throw the error I get, but still the plot is not created.

How can I clear out an existing plot and then create a new plot in the same div?

Upvotes: 2

Views: 1650

Answers (1)

Larry Martell
Larry Martell

Reputation: 3756

Answering my own question:

If I use Plotly.newPlot instead of Plotly.plot it works.

Upvotes: 4

Related Questions