matplotlib in Jupyter Vpython

267 views
Skip to first unread message

Brian Lancor

unread,
Oct 28, 2016, 2:55:30 PM10/28/16
to VPython-users
Hello Folks,
  Sorry if this has been answered before, but I haven't found a simple answer.  Is there a way to make vpython and matplotlib play nice in a jupyter notebook?  I am creating the plot objects successfully, but the show() command is running forever with no output.  I'd love to be able to use the vector obhect and the animations from Vpython without students having to learn a second set of commands for plotting.  
Thanks,
Brian

Bruce Sherwood

unread,
Oct 28, 2016, 3:39:27 PM10/28/16
to VPython-users
Can you provide a simple example that fails? I would have thought that one could use matplotlib with vpython. Maybe there's some kind of unanticipated conflict in message passing from Python to the browser?

Doug Blank

unread,
Oct 28, 2016, 3:44:21 PM10/28/16
to vpytho...@googlegroups.com
To use matplotlib with regular Python (eg, with IPython kernel) you must first:

%matplotlib notebook

or 

%matplotlib inline

Are you working with the CPython-based VPython? Or the in-browser (GlowScript) version?

-Doug

On Fri, Oct 28, 2016 at 3:39 PM, Bruce Sherwood <bruce.s...@gmail.com> wrote:
Can you provide a simple example that fails? I would have thought that one could use matplotlib with vpython. Maybe there's some kind of unanticipated conflict in message passing from Python to the browser?

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wayne

unread,
Nov 2, 2016, 9:44:34 PM11/2/16
to VPython-users
It definitely works along with VPython in a Jupyter notebook cell. I don't have any code with Vpython and matplotlib working in conjunction that I am at liberty to share right now, but I'll paste a bit of code that has both elements below and you can adapt it to make the plots rely on some aspect of VPython. 
You mentioned animations. Besides what Doug said, the thing I found for that is to use the camas.draw() function (not plt.show() or plot.draw()) to have it be updatable.

This works in the current VPython Binder:

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import time
from vpython import *


plt.style.use('ggplot')

def makeplots(ax1, ax2):
    ax1.clear()
    ax1.set_xlim(-3,3)
    ax1.set_ylim(-3,3)
    x, y = np.random.normal(size=(2, 10))
    ax1.plot(x, y, 'o')

    ax2.clear()
    ax2.set_xlim(-3,3)
    ax2.set_ylim(-3,3)
    i, j = np.random.normal(size=(2, 35))
    ax2.plot(i, j, 'x')

    fig.canvas.draw() 


fig, axes = plt.subplots(ncols=2, nrows=1)
ax1, ax2 = axes.ravel()

box()

for f in range(5):
    makeplots(ax1, ax2)
    time.sleep(0.5)

Aaron Titus

unread,
Nov 3, 2016, 9:56:30 AM11/3/16
to vpytho...@googlegroups.com
A fairly simple example is at:


However, this notebook is two years old and a lot has changed since then. For instance, I used the ivisual package (which predated the vpython package). However, if you import vpython instead of ivisual, I think the code will run.

I used matplotlib to plot data after the simulation’s while loop completed. Thus, I’m not plotting data in realtime with matplotlib. In general, when plotting realtime data, I use vpython's graphing functions. When using matplotlib, I (and my students) store data in lists and then plot the data after the simulation finishes. I don’t know if this is necessary or not. I haven’t tried plotting data in realtime with matplotlib.

Aaron

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.

Wayne

unread,
Nov 3, 2016, 3:36:52 PM11/3/16
to VPython-users
A made a gist of a realtime version. It can be found at https://gist.github.com/fomightez/4006607183950f2a3a9873102f83810d
I integrated Bruce's awesome AtomicSolid demonstration with a simple realtime matplotlib plot. I think the overhead of the two of them in the notebook slows things down a bit, but they do indeed run in animated realtime mode in a Jupyter notebook together. I was using the Binder package one, and so things may be smoother elsewhere.
If not animating the plotting, you can use the more standard `plt.show()` approach with the final data like in the link Aaron posted.

Wayne

unread,
Nov 8, 2016, 11:01:57 AM11/8/16
to VPython-users
I saw that you can tag interesting programs that you'd like others to see with `VPDEMO:`, and so I was trying to add to subject line that so it may be found by more folks that way.
Reply all
Reply to author
Forward
0 new messages