Fill area between 2 lines [SOLVED]

Hey all,

I am trying to fill the area between 2 lines that are crossing at a random point.

I’ve tried adding multiple traces and different fill options but I can’t figure out how to achive the below result.

Thank you,
Alex

Hi. I think if you are using pandas you can get the portion of data for each crossing point and then fill the area for each portion. Then, append all the fill individual areas in one graph. I know is not the best solution but, it could be a good start.

Hi, thank you for the fast reply. If was very helpful.

I managed to achive what I was looking for. See below:

What I did I create a 3rd line “-------” (‘senkou_c’). I calculated the line below like that.

		df['senkou_c'] = np.nan
		for index, row in df.iterrows():
			if row['senkou_a'] < row['senkou_b']:
				df.at[index,'senkou_c'] = row['senkou_a']
			elif row['senkou_a'] > row['senkou_b']:
				df.at[index,'senkou_c'] = row['senkou_b']
			elif row['senkou_a'] == row['senkou_b']:
				df.at[index,'senkou_c'] = row['senkou_b']

And then I plotted the like below:

		if self.df.__contains__('senkou_c'):
			trace = go.Scatter(
				x = self.df['date'],
				y = self.df['senkou_c'],
				name = "------",
				line = dict(color = ('rgba(255, 255, 255, 0)')))
			self.data.append(trace)

		if self.df.__contains__('senkou_b'):
			trace = go.Scatter(
				x = self.df['date'],
				y = self.df['senkou_b'],
				fill = 'tonexty',
				name = "Senkou B",
				fillcolor = 'rgba(255, 0, 0, 0.3)',
				line = dict(color = ('rgba(255, 0, 0, 0.3)')))
			self.data.append(trace)

		if self.df.__contains__('senkou_c'):
			trace = go.Scatter(
				x = self.df['date'],
				y = self.df['senkou_c'],
				name = "------",
				line = dict(color = ('rgba(255, 255, 255, 0)')))
			self.data.append(trace)

		if self.df.__contains__('senkou_a'):
			trace = go.Scatter(
				x = self.df['date'],
				y = self.df['senkou_a'],
				fill = 'tonexty',
				name = "Senkou A",
				fillcolor = 'rgba(0, 255, 0, 0.3)',
				line = dict(color = ('rgba(0, 255, 0, 0.3)')))
			self.data.append(trace)

I hope this will help other people looking for something similar.

Thank you,
Alex

1 Like

Alex, thank you. I was having problems shading the positive and negative areas of Ichimoku clouds and of ADX DI+/DI- of OHLC+V data.
I was expecting being able to choose a fill color based on a conditional, i.e, fillcolor = np.where(condition, color A, color B), but alas this isn’t possible.

It would be fantastic if the PlotLy developers inplemented this ability since it’s of tremendous use in quantitative finance, econometrics, technical analysis, and i suspect for much more.

1 Like

I tried this method, the problem is when there is the transition from one line to another, there is an error. I think the figure shows what’s happening

markers have this ablity. but them cant fill an area. :_)


got this weird triangles if data is a little bit large, and crossover fill color error happened as well, wonder if anyone has better solution? ths.

Hi @bin1482, welcome to the forums.

I think it’s going to be almost impossible to help you without your data.

@AIMPED ,after checking the data,figured out when line A equals to line B,i gave C the value of np.nan, that caused the weird problem. Now everything is ok, thx for ur reply.

1 Like

Could you post the code that worked for you please?