matplotlib: plotting in a loop, setting/getting plot data

Issue #886 new
Nico Schlömer created an issue

I've started playing around with the matplotlib plotting interface and I love it. Finally, proper subplots in one window!

When plotting data that changes over time, I'm doing something along the lines of

# plot initial states
pp.figure()
pp.subplot(1, 3, 1)
plot(u0)
pp.subplot(1, 3, 2)
plot(p0)
pp.subplot(1, 3, 3)
plot(theta0)

while t < end_time:
     # compute

     pp.subplot(1, 3, 1)
     plot(u0)
     pp.subplot(1, 3, 2)
     plot(p0)
     pp.subplot(1, 3, 3)
     plot(theta0)
     pp.show(block=False)
     pp.pause(0.05)

That works.

There are however other, more efficient ways of plotting varying data without block=False and pause. One is to set the data on an existing figure. For pyplot.plot, this works like

# plot initial states
pp.figure()
pp.subplot(1, 3, 1)
data0 = pp.plot(u0)[0]
pp.subplot(1, 3, 2)
data1 = pp.plot(p0)[0]
pp.subplot(1, 3, 3)
data2 = plot(theta0)[0]

while t < end_time:
     # compute

     data0.set_data(u0_data)
     data1.set_data(p0_data)
     data2.set_data(theta0_data)
     fig.canvas.draw()

If I see correctly, this isn't possible in Dolfin yet. Two things are missing:

  1. plot doesn't return the mpl data object.
  2. There's no way get the mpl data from a Function yet.

Ideas?

Comments (2)

  1. Prof Garth Wells

    There are some serious shortcomings with the present plotting implementation. Any contribution would be welcome! The plan is to remove the VTK backend, so the sooner Matplotlib is in shape, the sooner VTK can go. The is a related PR at https://bitbucket.org/fenics-project/dolfin/pull-requests/319/default-to-matplotlib-for-plotting-from/diff.

    We had a discussion on the interface to Matplolib a while back on Slack, so it will have disappeared in the history vacuum by now. An identified issue was that plot doesn't return a plot object. More problematic, the behaviour of plot differs fundamentally depending on the backend (Matplotlib vs VTK), which is a very bad design error.

  2. Nico Schlömer reporter

    The plan is to remove the VTK backend,

    That's good to hear. IMHO, it's good enough for Dolfin to have basic plotting capabilities; for everything beyond that, there are software projects dedicated to only graphic representation (ParaView, mayavi, visit, tecplot, etc.).

    More problematic, the behaviour of plot differs fundamentally depending on the backend (Matplotlib vs VTK), which is a very bad design error.

    I would think Dolfin's VTK interface and matplotlib are so different that it will be nearly impossible to condense them into a single interface. Is that necessary at all though? Transitioning to mpl could be as simple as providing a get_mpl_data() function.

    pp.figure()
    data0 = pp.plot(get_mpl_data(u0))[0]
    

    If necessary, pp.plot(get_mpl_data(u0))[0] could be condensed to mpl_plot(u0), plot() could be deprecated, removed, and mpl_plot be renamed to plot an iteration later.

  3. Log in to comment