2

I've been playing around with mpld3 plotting and am trying to figure out a good way of updating the generated mpld3 plots by supplying a new JSON serialized dictionary. Let's say I've generated my mpld3 plot using some templating engine:

var json_data = {{ mpld3 json serialized using mpld3.fig_to_dict() }}
mpld3.draw_figure("fig01", json_data);

Now I've added a slider with a callback which is meant to update the entire figure using a new JSON generated using the mpld3.fig_to_dict() call. This PR on mpld3's Github demonstrates how to update and animate individual line elements using a slider but is there any simple way to simply update the entire figure by supplying new json data?

Abraham D Flaxman
  • 2,969
  • 21
  • 39
philippjfr
  • 3,997
  • 14
  • 15

1 Answers1

2

It seems like the best way to update the entire figure is to remove everything and start fresh. In terms of your code above:

var json_data = {{ mpld3 json serialized using mpld3.fig_to_dict() }};
d3.select("#fig01").selectAll("*").remove();
mpld3.draw_figure("fig01", json_data);

Here is a little example gist.

Abraham D Flaxman
  • 2,969
  • 21
  • 39
  • 1
    I figured as much, I realize it's a bit much for me to expect mpld3 to magically figure out which elements to update, delete etc. Redrawing probably remains my only options. Thanks a bunch! – philippjfr Oct 29 '14 at 14:52
  • 1
    I wish that it would, and I invite you to [add a feature request about it](https://github.com/jakevdp/mpld3/issues). Unfortunately, I think it will be a bit complicated to implement. – Abraham D Flaxman Oct 30 '14 at 17:21