Skip to content

--legend option to gwpy-plot is unused

As reported by @borjasor, the --legend option to gwpy-plot ends up doing nothing. This is because the args.legend parameter isn't referenced when the legend is created:

https://github.com/gwpy/gwpy/blob/96ecf1fd701c681427a2f9b64437388c7842c04c/gwpy/cli/cliproduct.py#L630-L639

I think something like the following might work to fix this, I will test and post a PR:

diff --git a/gwpy/cli/cliproduct.py b/gwpy/cli/cliproduct.py
index 0c928ccf..20ef4f92 100644
--- a/gwpy/cli/cliproduct.py
+++ b/gwpy/cli/cliproduct.py
@@ -547,7 +547,7 @@ class CliProduct(object):
         """Finalize figure object and show() or save()
         """
         self.set_axes_properties()
-        self.set_legend()
+        self.set_legend(*self.args.legend)
         self.set_title(self.args.title)
         self.set_suptitle(self.args.suptitle)
         self.set_grid(self.args.nogrid)
@@ -627,10 +627,11 @@ class CliProduct(object):
         """
         self._set_axis_properties('y')

-    def set_legend(self):
+    def set_legend(self, *labels):
         """Create a legend for this product (if applicable)
         """
-        leg = self.ax.legend(prop={'size': 10})
+        handles, labels_ = self.ax.get_legend_handles_labels()
+        leg = self.ax.legend(handles, labels or labels_, prop={'size': 10})
         if self.n_datasets == 1 and leg:
             try:
                 leg.remove()

cc @areeda