Skip to content
Snippets Groups Projects
Commit b3449155 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

add support for _custom style_ in `plot.py` (dragoon/komodo!90)

> :bulb: **Note**  
> - in the following examples, any part of the `$.style` specification is optional and can either be ommitted or set to `null`
> - the default values for `$.style` are given in `plot.py --help`

```rust
let xs = seq 0 5 | each { 2 ** $in } | wrap x
let twice = $xs | insert measurement { 2 * $in.x } | insert error { 0.1 + 0.5 * $in.x }
let square = $xs | insert measurement { $in.x ** 2 } | insert error { 1 + 1.5 * $in.x }
```
and try
```rust
python scripts/plot/plot.py --title title --x-label x --y-label y --fullscreen ([
    { group: "x ^ 2", items: $square },
    { group: "2 * x", items: $twice }
] | to json)
```
vs
```rust
python scripts/plot/plot.py --title title --x-label x --y-label y --fullscreen ([
    {
        group: "x ^ 2",
        items: $square,
        style: {
            color: "red",
            alpha: 0.5,
            line: {
                marker: 's',
                width: 2,
                type: "dashed",
            },
        }
    },
    {
        group: "2 * x",
        items: $twice,
        style: {
            color: "purple",
            alpha: 0.1,
            line: {
                marker: 'o',
                width: 5,
                type: "dotted",
            },
        }
    }
] | to json)
```
parent 72f3cd8a
No related branches found
No related tags found
No related merge requests found
......@@ -4,36 +4,56 @@ import sys
import matplotlib.pyplot as plt
import argparse
HELP = """## Example
```nuon
[
{
group: "Alice",
items: [
[ x, measurement, error ];
[ 1, 1143, 120 ],
[ 2, 1310, 248 ],
[ 4, 1609, 258 ],
[ 8, 1953, 343 ],
[ 16, 2145, 270 ],
[ 32, 3427, 301 ]
],
style = {}, # optional, see section below
},
{
group: "Bob",
items: [
[ x, measurement, error ];
[ 1, 2388, 374 ],
[ 2, 2738, 355 ],
[ 4, 3191, 470 ],
[ 8, 3932, 671 ],
[ 16, 4571, 334 ],
[ 32, 4929, 1094 ]
]
style = {}, # optional, see section below
},
]
```
## Custom style
any record inside the data can have an optional "style" specification.
below is the full shape of that specification, where all of the keys are completely optional,
default values have been chosen:
```nuon
{
color: null, # see https://matplotlib.org/stable/users/explain/colors/colors.html
line: {
marker: "o", # see https://matplotlib.org/stable/api/markers_api.html
type: null, # see https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html
width: null, # just an integer
}
alpha: 0.3, # a real number between 0 and 1
}
```"""
# # Example
# ```nuon
# [
# {
# group: "Alice",
# items: [
# [ x, measurement, error ];
# [ 1, 1143, 120 ],
# [ 2, 1310, 248 ],
# [ 4, 1609, 258 ],
# [ 8, 1953, 343 ],
# [ 16, 2145, 270 ],
# [ 32, 3427, 301 ]
# ]
# },
# {
# group: "Bob",
# items: [
# [ x, measurement, error ];
# [ 1, 2388, 374 ],
# [ 2, 2738, 355 ],
# [ 4, 3191, 470 ],
# [ 8, 3932, 671 ],
# [ 16, 4571, 334 ],
# [ 32, 4929, 1094 ]
# ]
# },
# ]
# ```
# see [`HELP`]
def plot(
data,
title: str,
......@@ -54,9 +74,26 @@ def plot(
down = [y - z for (y, z) in zip(ys, zs)]
up = [y + z for (y, z) in zip(ys, zs)]
style = "dashed" if group["group"].endswith("-ark") else "solid"
ax.plot(xs, ys, label=group["group"], marker='o', linestyle=style)
ax.fill_between(xs, down, up, alpha=0.3)
style = {
"marker": 'o',
"linestyle": None,
"color": None,
"linewidth": None,
}
alpha = 0.3
if "style" in group:
custom_style = group["style"]
style["color"] = custom_style.get("color", None)
style["marker"] = custom_style.get("line", {}).get("marker", style["marker"])
style["linestyle"] = custom_style.get("line", {}).get("type", style["linestyle"])
style["linewidth"] = custom_style.get("line", {}).get("width", style["linewidth"])
alpha = custom_style.get("alpha", alpha)
ax.plot(xs, ys, label=group["group"], **style)
if style["color"] is None:
ax.fill_between(xs, down, up, alpha=alpha)
else:
ax.fill_between(xs, down, up, alpha=alpha, color=style["color"])
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
......@@ -79,36 +116,11 @@ def plot(
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("data", type=str, help="""
the actual data to show in a multibar plot, here is an example:
[
{
group: "Alice",
items: [
[ x, measurement, error ];
[ 1, 1143, 120 ],
[ 2, 1310, 248 ],
[ 4, 1609, 258 ],
[ 8, 1953, 343 ],
[ 16, 2145, 270 ],
[ 32, 3427, 301 ]
]
},
{
group: "Bob",
items: [
[ x, measurement, error ];
[ 1, 2388, 374 ],
[ 2, 2738, 355 ],
[ 4, 3191, 470 ],
[ 8, 3932, 671 ],
[ 16, 4571, 334 ],
[ 32, 4929, 1094 ]
]
},
]
""")
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("data", type=str, help=f"the actual data to show in a multibar plot\n\n{HELP}"
)
parser.add_argument("--title", "-t", type=str, help="the title of the plot")
parser.add_argument("--x-label", "-x", type=str, help="the x label of the plot")
parser.add_argument("--y-label", "-y", type=str, help="the y label of the plot")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment