From d759b4e532d203e511bdb68021180039cffd9df3 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 23 Aug 2021 21:04:25 -0400 Subject: [PATCH] motan_graph: Fix legend when using matplotlib twinx Signed-off-by: Kevin O'Connor --- scripts/motan/motan_graph.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/motan/motan_graph.py b/scripts/motan/motan_graph.py index b593c1ce..9826f1a2 100755 --- a/scripts/motan/motan_graph.py +++ b/scripts/motan/motan_graph.py @@ -53,8 +53,13 @@ def plot_motion(amanager, graphs): pparams = {'label': label['label'], 'alpha': 0.8} pparams.update(plot_params) ax.plot(times, datasets[dataset], **pparams) - ax.legend(loc='best', prop=fontP) - ax.grid(True) + if twin_ax is not None: + li1, la1 = graph_ax.get_legend_handles_labels() + li2, la2 = twin_ax.get_legend_handles_labels() + twin_ax.legend(li1 + li2, la1 + la2, loc='best', prop=fontP) + else: + graph_ax.legend(loc='best', prop=fontP) + graph_ax.grid(True) rows[-1].set_xlabel('Time (s)') return fig