I often have a 2-column x-y data set that I want to plot as y vs x.  The 
attached script does this fairly easily, with minimal setup.  You do have 
to have installed Matplotlib.

The data must be in a node of its own, and be configured like a config 
file.  At a minimum there must be a [data] section].  Optionally there may 
be a [labels] or a [config] section.

The script can configure the plot appearance by loading a Matplotlib style 
file.  This part is optional, as controlled by an optional [config] 
section.  The code as provided looks for a file called *local_mplstyle*  in 
Python's *site.getuserbase()* directory.  If you like to configure the 
appearance of Matplotlib plots some other way, then change the code or just 
ignore it.

The script is extensively commented.  To try it out, load the attached Leo 
outline.  Select the *Quickplot* node and press the *script button* button.  
Select the *data*  node, then press the new *Quickplot* button.

This script is pretty new and has no error handling.  Let me know if you 
think it should work differently, or feel free to modify it.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/e8aa6df1-0c9d-4866-a5bc-7994afe9e1f4n%40googlegroups.com.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1"; >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="tom.20210903160640.1"><vh>Plot data config-file style</vh>
<v t="tom.20210903160703.1"><vh>data</vh></v>
<v t="tom.20210903172405.1"><vh>Quickplot</vh>
<v t="tom.20210903172405.2"><vh>parse_data()</vh></v>
<v t="tom.20210903172405.3"><vh>&lt;&lt; set plot styles &gt;&gt;</vh></v>
</v>
</v>
</vnodes>
<tnodes>
<t tx="tom.20210903160640.1"></t>
<t tx="tom.20210903160703.1">[data]
data = 1 1
    2 2
    3 4
    # comment
    ; comment
    ; No blank lines, please!
    4 16
    5 32

[labels]
title = Plot Example
xaxis = Days
yaxis = Values

[config]
configure_plot =</t>
<t tx="tom.20210903172405.1">@language python
"""Plot 2-column x-y data from a data node.

   The data node must contain sections as in a config file.  
   Currently three sections are used: [data], [labels], and
   [config]. The [data] section MUST look like this:

data = 1 1
    2 2
    3 4
    # comment
    ; comment
    ; No blank lines, please!
    ; etc.

    All lines after the first must be indented as shown.  This causes
    the config parser to treat them as a single multi-line string.
    
    The name "data" is required.

    The [labels] section may contain the following keys:

[labels]
title = 
xaxis = 
yaxis = 

    The optional [config] section may contain the key "configure_plot".
    If this key has the value True, then the procedure in the 
    &lt;&lt; set plot styles &gt;&gt; section will be called, otherwise not.
    
    This script requires Matplotlib to have been installed.
    
    To use this script, attach it to a button or command, select
    the data node, and activate the script.
"""

import site
import os.path
import configparser
from matplotlib import pyplot as plt

&lt;&lt; set plot styles &gt;&gt;
@others

config = configparser.ConfigParser()
config.read_string(c.p.b)

do_config = config['config'].get('configure_plot', False) == 'True'
if do_config:
    set_plot_styles()

data = config['data']['data']
x, y = parse_data(data)

labels = config['labels']
title = labels.get('title')
xaxis = labels.get('xaxis')
yaxis = labels.get('yaxis')
legend = labels.get('legend')

if title: plt.title(title)
if xaxis: plt.xlabel(xaxis)
if yaxis: plt.ylabel(yaxis)
if legend: plt.legend(legend)


# Plot
plt.plot(x,y)
plt.show()
</t>
<t tx="tom.20210903172405.2">def parse_data(data):
    """Return x, y sequences from 2-column string data.
    
    RETURNS
    a tuple (x, y) of data sequences."""

    lines = [line for line in data.split('\n')]

    xy = [line.split() for line in lines]
    xs, ys = zip(*xy)
    x = [float(a) for a in xs]
    y = [float(b) for b in ys]

    return x, y</t>
<t tx="tom.20210903172405.3">def set_plot_styles():
    style_dir=site.getuserbase()
    style_file=os.path.join(style_dir,'local_mplstyle')
    plt.style.use(style_file)
</t>
</tnodes>
</leo_file>

Reply via email to