Skip to content

Commit 679591d

Browse files
committed
Enhance plotting functions to support saving plots to files and fix bug using integer devision instead of floating point devision
1 parent 9d4d4e2 commit 679591d

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

plot_data.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
def convert_timestamp(timestamp_ms):
1212
"""Converts Unix timestamp in milliseconds to a readable datetime format"""
13-
timestamp_sec = timestamp_ms // 1000
13+
timestamp_sec = timestamp_ms / 1000
1414
dt = datetime.fromtimestamp(timestamp_sec)
1515
return dt
1616

1717

18-
def plot_raw_data(csv_file, value_name='Value'):
18+
def plot_raw_data(csv_file, value_name='Value', out_file=None):
1919
"""Plots raw data values"""
2020

2121
print(f"Loading raw data from {csv_file}...")
@@ -40,10 +40,14 @@ def plot_raw_data(csv_file, value_name='Value'):
4040
plt.xticks(rotation=45, ha='right')
4141
plt.tight_layout()
4242

43-
plt.show()
43+
if out_file:
44+
plt.savefig(out_file)
45+
print(f"Plot saved to {out_file}")
46+
else:
47+
plt.show()
4448

4549

46-
def plot_anomalies(csv_file, value_name='Value', show_anomaly_values=False):
50+
def plot_anomalies(csv_file, value_name='Value', show_anomaly_values=False, out_file=None):
4751
"""Plots data with marked anomalies and background shading"""
4852

4953
print(f"Loading anomaly data from {csv_file}...")
@@ -123,7 +127,11 @@ def plot_anomalies(csv_file, value_name='Value', show_anomaly_values=False):
123127
plt.xticks(rotation=45, ha='right')
124128
plt.tight_layout()
125129

126-
plt.show()
130+
if out_file:
131+
plt.savefig(out_file)
132+
print(f"Plot saved to {out_file}")
133+
else:
134+
plt.show()
127135

128136

129137
def main():
@@ -150,6 +158,8 @@ def main():
150158
help='Name of the values being plotted (e.g., CPU Usage, Memory, Temperature)')
151159
parser.add_argument('--show-anomaly-values', action='store_true',
152160
help='Show anomaly scores on a secondary y-axis')
161+
parser.add_argument('--out', type=str, metavar='FILE',
162+
help='Output file to save the plot')
153163

154164
args = parser.parse_args()
155165

@@ -162,10 +172,10 @@ def main():
162172

163173
# Plot data
164174
if args.raw:
165-
plot_raw_data(args.raw, args.value_name)
175+
plot_raw_data(args.raw, args.value_name, args.out)
166176

167177
if args.anomalies:
168-
plot_anomalies(args.anomalies, args.value_name, args.show_anomaly_values)
178+
plot_anomalies(args.anomalies, args.value_name, args.show_anomaly_values, args.out)
169179

170180

171181
if __name__ == "__main__":

0 commit comments

Comments
 (0)