# tep code ###### tags: `code` - matplotlib for visulization ```python= import pandas as pd import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button , RadioButtons, RangeSlider #read raw data df = pd.read_csv(r"C:\Users\siaje\OneDrive\Desktop\Caloudi\code\data\sample data\MTS.csv") df = df.reset_index(inplace=False) #read ground truth gt = pd.read_csv(r"C:\Users\siaje\OneDrive\Desktop\Caloudi\code\data\sample data\MTS-gth.csv") col = df.columns.to_list() fig = plt.figure(figsize=(14, 14), dpi=72) ax = [] for i in range(1, len(col)): #Make subplots with sharex if i == 1 : ax.append(plt.subplot(len(col), 1, i)) else: ax.append(plt.subplot(len(col), 1, i, sharex = ax[0])) plt.plot(df['index'], df[col[i]], color="#542de0") plt.title(col[i], fontsize=12, loc="right") #Subplot Name #declare grid and place behind everything else ax[i-1].set_axisbelow(True) ax[i-1].grid(True, color="#ffffff", linewidth=2) #Set y-limit min_range = min(df[col[i]])-10 max_range = max(df[col[i]])+10 plt.ylim(min_range, max_range) #Aestheticc: Hide outside border and set background color ax[i-1].spines['top'].set_visible(False) ax[i-1].spines['bottom'].set_visible(False) ax[i-1].spines['left'].set_visible(False) ax[i-1].spines['right'].set_visible(False) ax[i-1].set_facecolor("#ebe9f5") #Annotate Anomaly start = -1 for i in range(len(gt)): if gt['label'][i] == 1 and start == -1: start = i elif gt['label'][i] == 0 and start != -1: [j.axvspan(start-20, i+20, facecolor='r', alpha=0.5) for j in fig.get_axes()] start = -1 #Slider #axmin = fig.add_axes([0.25, 0, 0.65, 0.03], facecolor="blue") #axmax = fig.add_axes([0.25, 0.05, 0.65, 0.03], facecolor="red") range_slider = fig.add_axes([0.25, 0, 0.65, 0.03], facecolor="red") #smin = Slider(axmin, "Min", 0, len(df["index"]), valinit=0) #smax = Slider(axmax, "Max", 0, len(df["index"]), valinit=len(df["index"])) slider = RangeSlider(range_slider, "Range", 0, len(df["index"])) def update(val): fig.xlim(slider.val[0], slider.val[1]) fig.canvas.draw_idle() #smin.on_changed(update) #smax.on_changed(update) slider.on_changed(update) #plt.tight_layout() plt.show() ```