New Command To Plot Data From Clipboard

61 views
Skip to first unread message

tbp1...@gmail.com

unread,
Nov 8, 2021, 8:10:42 PM11/8/21
to leo-editor
I have adapted the code for Viewrendered3's new ability to plot data in nodes, and with minor changes it functions as a stand-alone command to take data from the clipboard and plot it.  So if you get one- or two-column data into the clipboard you can take a quick look at it.

Here is an example of X-Y data:

    1 1
    2 .5
    3 -6
    # comment
    ; comment

    4 -16
    5 -2
    6  5
    7  10

Non-numeric lines are ignored except for special configuration sections, so you can copy a large range of data sources and plot them instantly.  You could also do some computation in a node that you run with CTRL-B, and add a few lines to format it and copy it to the clipboard. I have found that ability to be very useful while I've developed code.  The script is very flexible and robust (so far as I know, anyway).

You do need to install pyperclip (for getting the clipboard), and matplotlib to do the actual plotting - you probably have it installed already. You could change the script to use Leo's own clipboard code or some other clipboard package;  I just happen to like pyperclip and think it's easy to use.

As of this evening, the script is in scripts.leo in the devel  branch, under a new node with the headline Plots and Graphs.  More information and usage details are in the docstring of the script, and the new Help for plot-2d menu item in VR3 is very applicable since the code is largely the same.

tbp1...@gmail.com

unread,
Nov 11, 2021, 10:02:04 PM11/11/21
to leo-editor
If you have a node that reads or computes some X-Y data, you can add axis labels and a title to your clipboard plot by protecting the [labels] section with and "@" directive.  This causes the following text to be ignored when the script is executed by CNTL-B of with VR3, but it does not hide the section from the plot clipboard command.  Here is an example.  Let's read annual global surface temperature from a file, then plot it.  The file will contain rows like this:

1900   0.395
1901   0.438
# etc.

The code and the [labels] section could look like this:

import pyperclip
datafile = r'c:\data\sst.txt'
with open(datafile) as file:
    data = file.read()
pyperclip.copy(data)

@
[labels]
title = Global Surface Temperature Vs Year
xaxis = Year
yaxis = "Global Average Surface Temperature, Deg C.

When you run this node, say with CTRL-B, the file will be read and its contents copied to the clipboard.  When you invoke the plotting command, the data in the clipboard will be plotted, and the graph's title and axis labels will be taken from the [labels] section of the node.  The "@" directive prevents the section from being interpreted as code.

zhaohe wang

unread,
Nov 13, 2021, 8:53:30 AM11/13/21
to leo-editor

1. pip install pyperclip

2. git pull

3. copy example data

4. Ctrl+B

return: no script selected

Is there anything wrong with my operation?

tbp1...@gmail.com

unread,
Nov 13, 2021, 11:44:15 AM11/13/21
to leo-editor
 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. 
example_plot.png

tbp1...@gmail.com

unread,
Nov 13, 2021, 4:48:46 PM11/13/21
to leo-editor
I have added a section to the docstring of the command to cover how to install it and bind it to a key, button, or menu .  The update is available on the latest 6.6 branch, in scripts.leo.

zhaohe wang

unread,
Dec 5, 2021, 6:41:56 AM12/5/21
to leo-editor
Thanks a lot for your detailed description.
Reply all
Reply to author
Forward
0 new messages