Want to make static SVG plot (from matplotlib) interactive via the browser

前端 未结 4 566
生来不讨喜
生来不讨喜 2021-02-05 20:56

My goal is to make a plot that you can interact with in the browser. Ideally, I would like a well-documented and mature JavaScript plotting library that supports SVG. As far as

4条回答
  •  清歌不尽
    2021-02-05 21:02

    One way to this can be seen in the matplotlib gallery.

    Basically:

    1. In matplotlib, use element.set_gid("youridhere") on the matplotlib element you wish to make interactive. That is, use set_gid() on the output from plot()/hist()/whatever().
    2. Create an svg with matplotlib, but use a StringIO object as your file.
    3. Parse the svg with an xml library (e.g. xml.etree.ElementTree)
    4. Find the xml elements with the id that you set (e.g. "youridhere").
    5. Add onclick/on${theeventyoucareabout} attributes with a javascript function name.
    6. Add a script element with your javascript as a CDATA to the xml tree.
    7. Export the xml to an svg file!

提交回复
热议问题