Home > Enterprise >  problem on filing up the colour between two index values
problem on filing up the colour between two index values

Time:10-03

I have a timeseries data timeseries.txt. First I select a index value (here 50) and put a red line mark on that selected index value. And I want to highlight portion before(idx-20) and after(idx 20) the red line index value on the timeseries.

I wrote this code however i am able to put the red line mark on the timeseries but while using fill_betweenx it doesnot work. I hope experts may help me overcoming this problem.Thanks.

import matplotlib.pyplot as plt
import numpy as np

input_data=np.loadtxt("timeseries.txt")
time=np.arange(len(input_data))
plt.plot(time,input_data)
idx = [50]
mark = [time[i] for i in idx]
plt.plot(idx,[input_data[i] for i in mark], marker="|",color='red',markerfacecolor='none',mew=0.4,ms=30,alpha=2.0)
plt.fill_betweenx(idx-20,idx 20 alpha=0.25,color='lightsteelblue')
plt.show()

enter image description here

CodePudding user response:

If you are looking for just a semi-transparent rectangle, you can use patches.Rectangle to draw one. Refer enter image description here

EDIT Please refer to the Rectangle documentation provided earlier in the response. You will need to adjust the start coordinates (x,y) and the height and width to see how big/small you need the Rectangle. For eg: changing the rectangle code like this...

plt.gca().add_patch(Rectangle((idx[0]-10, -0.40), 20, 0.8, facecolor = 'lightsteelblue',fill=True,alpha=0.25, lw=0))

will give you this plot.

enter image description here

  • Related