2

I have a sequence of anomalies point structured as [[10, 20], [23, 38], [45, 56]]. What I wanna do is try to plot them highlighting them as in the following plot (extracted from the a paper): enter image description here

Are there libraries to make it? At the moment I'm doing it point by point, but as you can see from the screen and from the code I'm just plotting the time series with different colors, based on where the anomalies are.

fig = plt.figure(figsize=(25,5)) 
ax1=plt.subplot(121)
sns.lineplot(data=df['values'], ax=ax1) # plot normal time series plot
sns.lineplot(data=df['values'][(df['anomalies'] == True )], color='red', ax=ax1)

enter image description here

Fabio
  • 635
  • 6
  • 17

1 Answers1

2

You can do what you need with plt.axvspan:

import matplotlib.pyplot as plt

# create some dummy data
data = {'a': np.arange(50),
        'c': np.random.randint(0, 50, 50),
        'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

# plot data
plt.scatter('a', 'b', c='c', s='d', data=data)

# draw highlighted areas
plt.axvspan(10, 15, color='red', alpha=0.5)
plt.axvspan(30, 35, color='red', alpha=0.5)
plt.axvspan(40, 42, color='green', alpha=0.5)

plt.show()

result:

enter image description here

BlackMath
  • 1,708
  • 1
  • 11
  • 14