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

allow to pass multiple legends

parent 7950264f
Branches
Tags
No related merge requests found
......@@ -70,8 +70,9 @@ if __name__ == "__main__":
"upper center",
"center"
],
default=None,
help="the location of the legend"
nargs="*",
default=[],
help="the locations of the legends"
)
plot_parser.add_argument("--no-legend", action="store_true")
plot_parser.add_argument("--x-label", "-x", type=str, help="the x label of the plot")
......
......@@ -5,6 +5,7 @@ import matplotlib
import numpy as np
import argparse
import shutil
from itertools import zip_longest
from error import GPLTError
from log import panic, warning
......@@ -36,6 +37,7 @@ class Style(TypedDict):
class Graph(TypedDict):
name: Optional[str]
legend: Optional[str]
points: List[Point]
style: Optional[Style]
......@@ -73,6 +75,7 @@ HELP = """## Example
[
{
name: "Alice", # optional, unset or set to null won't show the grap name
legend: "main", # optional, not required for simple plots
points: [
[ x, y, e ];
[ 1, 1143, 120 ],
......@@ -86,6 +89,7 @@ HELP = """## Example
},
{
name: "Bob", # optional, unset or set to null won't show the grap name
legend: "main", # optional, not required for simple plots
points: [
[ x, y, e ];
[ 1, 2388, 374 ],
......@@ -260,7 +264,7 @@ def plot(
x_label: str,
y_label: str,
plot_layout: str = "constrained",
legend_loc: str = None,
legend_loc: List[str] = [],
show_legend: str = True,
x_scale: str = "linear",
x_scale_base: int = None,
......@@ -293,8 +297,14 @@ def plot(
fig, ax = plt.subplots(layout=plot_layout)
legends = {}
for i, g in enumerate(graphs):
_plot_single(g, i, ax)
line = _plot_single(g, i, ax)
legend = g.get("legend", None)
if legend is not None:
if legend not in legends:
legends[legend] = []
legends[legend] += [line]
# ======================== axes ========================
......@@ -361,7 +371,14 @@ def plot(
ax.set_title(title)
if show_legend:
ax.legend(loc=legend_loc)
ax.legend(loc=legend_loc[0])
extra_legends = []
for loc, handles in zip_longest(legend_loc, legends.values()):
if handles is not None:
extra_legends.append(ax.legend(handles=handles, loc=loc))
for legend in extra_legends[:-1]:
plt.gca().add_artist(legend)
return fig
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment