-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_signal_interactive.py
More file actions
65 lines (52 loc) · 2.05 KB
/
plot_signal_interactive.py
File metadata and controls
65 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go
# Use "matplotlib" or "plotly" as the plotting library
USE_PLOTLY = True # Change to False to use Matplotlib
# File path
input_file = "cropped_signal.txt"
def plot_signal_matplotlib(row, row_index):
"""Plots a signal row using Matplotlib with zoom and pan support."""
durations = list(map(int, row.strip().split(',')))
signal, time = [], []
current_time = 0
for duration in durations:
value = 1 if duration > 0 else 0
signal.extend([value] * abs(duration))
time.extend(range(current_time, current_time + abs(duration)))
current_time += abs(duration)
fig, ax = plt.subplots(figsize=(12, 4))
ax.step(time, signal, where='post')
ax.set_title(f"Signal Row {row_index} (Matplotlib Interactive)")
ax.set_xlabel("Time (Arbitrary Units)")
ax.set_ylabel("Signal")
ax.set_ylim(-0.2, 1.2)
plt.grid(True) # Enable grid for better visibility
plt.show() # Interactive mode allows zoom & pan
def plot_signal_plotly(row, row_index):
"""Plots a signal row using Plotly for smoother zooming & panning."""
durations = list(map(int, row.strip().split(',')))
signal, time = [], []
current_time = 0
for duration in durations:
value = 1 if duration > 0 else 0
signal.extend([value] * abs(duration))
time.extend(range(current_time, current_time + abs(duration)))
current_time += abs(duration)
fig = go.Figure()
fig.add_trace(go.Scatter(x=time, y=signal, mode='lines', name=f"Row {row_index}"))
fig.update_layout(
title=f"Signal Row {row_index} (Plotly Interactive)",
xaxis_title="Time (Arbitrary Units)",
yaxis_title="Signal",
yaxis=dict(range=[-0.2, 1.2]),
dragmode="pan", # Default mode is pan
)
fig.show()
# Read and plot each row
with open(input_file, "r") as file:
for i, line in enumerate(file):
if USE_PLOTLY:
plot_signal_plotly(line, i)
else:
plot_signal_matplotlib(line, i)