From 25650684551da0b9d9ff72e37d301365f7a3d577 Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Tue, 1 Nov 2022 00:42:23 -0700 Subject: [PATCH 1/2] add controls to subplot --- prometheus_api_client/metric.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prometheus_api_client/metric.py b/prometheus_api_client/metric.py index 095ac91..6031833 100644 --- a/prometheus_api_client/metric.py +++ b/prometheus_api_client/metric.py @@ -187,10 +187,10 @@ def __add__(self, other): error_string = "Different metric labels" raise TypeError("Cannot Add different metric types. " + error_string) - def plot(self): + def plot(self, *args, **kwargs): """Plot a very simple line graph for the metric time-series.""" if _MPL_FOUND: - fig, axis = plt.subplots() + fig, axis = plt.subplots(*args, **kwargs) axis.plot_date(self.metric_values.ds, self.metric_values.y, linestyle=":") fig.autofmt_xdate() # if matplotlib was not imported From 74e447a165b171d3685a6ce1b708bcc414ddf77d Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Tue, 1 Nov 2022 00:48:41 -0700 Subject: [PATCH 2/2] convert plot() so that it can be called more than once to show Metrics on the same graph --- prometheus_api_client/metric.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/prometheus_api_client/metric.py b/prometheus_api_client/metric.py index 6031833..8c57b20 100644 --- a/prometheus_api_client/metric.py +++ b/prometheus_api_client/metric.py @@ -91,6 +91,11 @@ def __init__(self, metric, oldest_data_datetime=None): self.start_time = self.metric_values.iloc[0, 0] self.end_time = self.metric_values.iloc[-1, 0] + if _MPL_FOUND: + # We store the matplotpy plot information as Class variables + Metric._fig = None + Metric._axis = None + def __eq__(self, other): """ Overloading operator ``=``. @@ -190,9 +195,21 @@ def __add__(self, other): def plot(self, *args, **kwargs): """Plot a very simple line graph for the metric time-series.""" if _MPL_FOUND: - fig, axis = plt.subplots(*args, **kwargs) - axis.plot_date(self.metric_values.ds, self.metric_values.y, linestyle=":") - fig.autofmt_xdate() + if not Metric._fig: + # One graph with potentially N lines - if plot() is called twice + Metric._fig, Metric._axis = plt.subplots(*args, **kwargs) + Metric._axis.plot_date(self.metric_values.ds, self.metric_values.y, + linestyle="solid", + label=str(self.metric_name) + ) + Metric._fig.autofmt_xdate() + # These are provided for documentation reasons only - it's presumptuous for this code to call them + # Metric._axis.set_xlabel('Date/Time') + # Metric._axis.set_ylabel('Metric') + # Metric._axis.set_title('Prometheus') + if len(Metric._axis.lines) > 1: + # We show a legend (or update the legend) if there's more than line on the plot + Metric._axis.legend() # if matplotlib was not imported else: raise ImportError("matplotlib was not found")