# Fill area between 2 lines \[SOLVED\]

**URL:** <https://community.plotly.com/t/fill-area-between-2-lines-solved/42865>\
**Category:** 📊 Plotly Python\
**Created:** [July 23, 2020, 5:21pm UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865 "2020-07-23T17:21:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nonickname](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nonickname/32/11939_2.png) [@nonickname](https://community.plotly.com/u/nonickname)\
**Post date:** [July 23, 2020, 5:21pm UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/1 "2020-07-23T17:21:07Z")

</div>

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.

 ![Figure_1](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/d/dbe15ce73d7aa0ee84105fbce140e3ae8c6113ca.png)

Thank you,  
Alex

---

<div class="post-metadata">

**Author:** ![danielT43](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/danielt43/32/12101_2.png) [@danielT43](https://community.plotly.com/u/danielT43)\
**Post date:** [July 24, 2020, 7:08am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/2 "2020-07-24T07:08:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nonickname](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nonickname/32/11939_2.png) [@nonickname](https://community.plotly.com/u/nonickname)\
**Post date:** [July 24, 2020, 1:40pm UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/3 "2020-07-24T13:40:03Z")

</div>

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

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

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/a2eca5fc0b6f73e596449d776966a7ba009bba54.png)

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

```auto
		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:

```auto
		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

---

<div class="post-metadata">

**Author:** ![tgi535](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tgi535](https://community.plotly.com/u/tgi535)\
**Post date:** [April 21, 2021, 11:34am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/4 "2021-04-21T11:34:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rabin](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rabin](https://community.plotly.com/u/rabin)\
**Post date:** [May 26, 2021, 9:43pm UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/5 "2021-05-26T21:43:50Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/1/15c7bef4fbb834ffca17d9cd9d9d4a3d0e39ecb0.png)

---

<div class="post-metadata">

**Author:** ![mablue](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/mablue/32/18748_2.png) [@mablue](https://community.plotly.com/u/mablue)\
**Post date:** [May 18, 2022, 5:26pm UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/6 "2022-05-18T17:26:53Z")

</div>

> [@tgi535](#):
>
> fillcolor = np.where(condition, color A, color B)

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

---

<div class="post-metadata">

**Author:** ![bin1482](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@bin1482](https://community.plotly.com/u/bin1482)\
**Post date:** [November 15, 2023, 4:40am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/7 "2023-11-15T04:40:55Z")

</div>

![Snipaste_2023-11-15_12-31-54](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/f/a/fafb473c0a2950c883989508555c145d3ffdfcec.png)  
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.

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [November 15, 2023, 11:22am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/8 "2023-11-15T11:22:48Z")

</div>

Hi @bin1482, welcome to the forums.

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

---

<div class="post-metadata">

**Author:** ![bin1482](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@bin1482](https://community.plotly.com/u/bin1482)\
**Post date:** [November 16, 2023, 1:48am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/9 "2023-11-16T01:48:46Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![nohedge](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nohedge/32/26943_2.png) [@nohedge](https://community.plotly.com/u/nohedge)\
**Post date:** [February 11, 2024, 4:10am UTC](https://community.plotly.com/t/fill-area-between-2-lines-solved/42865/10 "2024-02-11T04:10:29Z")

</div>

> [@nonickname](#):
>
> ```auto
> 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']
> 
> ```

Could you post the code that worked for you please?
