Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call plot more than once #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions prometheus_api_client/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``=``.
Expand Down Expand Up @@ -187,12 +192,24 @@ 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()
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")