This will be pre-writing for an entry in Leo's scripting miscellany
<http://leoeditor.com/scripting-miscellany.html>. It documents features
that Leo has had for a long time, but which I misunderstood myself until a
few days ago. Recent changes to the code add some protections, but have
not changed the code in fundamental ways.
@pyplot nodes start with @pyplot in the headline. The rest of the headline
is comments.
These nodes should contain matplotlib scripts that create figures or
animations. Like this:
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
images = []
for add in np.arange(15):
images.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30
)),))
animation = animation.ArtistAnimation(fig2, images,
interval=50,
repeat_delay=3000,
blit=True)
g.app.permanentScriptDict['animate_decay'] = animation
# Keep a python reference to the animation, so it will complete.
*Notes*
1. If the viewrendered (VR) pane is open, Leo will display the animation in
the VR pane whenever the user selects the @pyplot node. This has been
tested only with the viewrendered.py, not the viewrendered2.py plugin.
2. In addition to c, g, and p, the VR code predefines several other vars.
The VR code does the following imports:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
and then predefines the animation, matplotlib, np, numpy and plt vars *in
the @pyplot script.* numpy is also predefined as an alias for np.
*Displaying images externally*
The script above will display the animation in the VR pane itself, *provided
the VR pane is visible*. If you would like to display it in an external
window, don't put the script in an @pyplot node. Instead, put the script in
a regular node, with the following modifications:
1. Add the required imports:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
2. Place the following at the end of the
plt.ion()
# sets interactive mode. Prevents this message:
# QCoreApplication::exec: The event loop is already running
plt.show()
*Bugs*
1. Once you use the VR pane to display an image, *you can't display an
image externally*. I'm not sure this can easily be fixed.
2. The VR plugin will refuse to close the VR pane if it ever displays an
@pyplot image or animation. This prevents Leo from hard crashing in the
pyplot code. At present, I know of no way around this drastic solution,
but you can always resize the VR pane so it is effectively hidden.
*Summary*
@pyplot is a partial antidote to "d3 envy <https://d3js.org/>". It allows
any image or animation created by matplotlib <https://matplotlib.org/>to be
shown in a VR pane.
Make sure to save a reference to animations, so they will continue to work.
Regular Leonine scripts can display matplotlib images or animations in an
external Qt window, provided that the VR pane has not previously displayed
an @pyplot image. Once that happens, all matplotlib images will be
displayed in the VR pane, and the VR pane can't be closed, only hidden (by
hand).
Please try out @pyplot. There is plenty of time for improvements and bug
reports.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.