First, you need to install the command. To do that, copy the node from scripts.leo to myLeoSettings.leo under the
@settings tree and name the node
@command plot-2d-clipboard (you can name it anything you want). After restarting Leo (or maybe after only reloading settings, I'm not sure about that), you will have the command available.After you have data in the clipboard, you need to invoke the command. That would be ALT-X plot-2d-clipboard. That will plot the data. The node that you were on when you pressed CTRL-B probably didn't actually have a script. That is all right - you just didn't need to use CTRL-B because you already had the data in the clibpoard. Once the data is in the clipboard, it will be plotted by invoking the command and you don't need to run a script. If the selected node has a [labels] section, then the command will use the labels that are defined in the section.
You can also connect the command to a button by adding an @button node for it to myLeoSettings.leo. If you know how to add your own menu items, you can also connect the command to one of those with an @menu node (that is what I have done), or you could bind it to a key by means of the @shortcuts node in myLeoSettings.leo..
The only time you would need to run a script in a node is when you want to compute the data and then plot it. The script should end by copying the data to the clipboard. Here is a complete example of creating data, converting it to a string, and copying the data string to the clipboard. After executing the node with CTRL-B, you need to invoke the plotting command to actually plot it.
import pyperclip
x = range(10)
y = [z**2 for z in x]
result = '\n'.join([f'{xx} {yy}' for xx, yy in zip(x, y)])
pyperclip.copy(result)
@
[labels]
title = Example Plot
I have attached the plot I got from this node as a .png file.
You can even make this node plot itself by adding the following line after the data is copied to the clipboard:
c.k.simulateCommand('plot-2d-clipboard')
With this line, when you press CTRL-B, the curve will be computed, converted to the right string format, and plotted.
Remember - the "@" before the [labels] section is needed to prevent Leo from trying to execute that section (which it cannot do since it is not python code). Any text following the "@" line will be ignored by the CTRL-B command. If you do not include a section like that, you do not need the "@" either.
This is one of those things that are lengthy to explain but very simple to actually do. I am finding that I use the plotting command frequently.