Skip to content

Commit 231b1a7

Browse files
docs: explain tozeroy fills across missing data and show per-segment workaround (#5632)
Co-authored-by: Mutasim ur Rehman <motasimurrehman565@gmail.com>
1 parent 1526efd commit 231b1a7

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎doc/python/filled-area-plots.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,50 @@ fig.add_trace(go.Scatter(
129129
fig.show()
130130
```
131131

132+
#### Filled Areas with Missing Data
133+
134+
Missing values (`None` or `NaN`) break a line into separate segments unless `connectgaps=True` is set. Currently, `fill='tozeroy'` and `fill='tozerox'` still draw one filled shape for the whole trace, so the fill spans the gaps. This rendering behavior comes from plotly.js and is tracked in [plotly.js#2736](https://github.com/plotly/plotly.js/issues/2736).
135+
136+
To keep the gaps unfilled, you can draw each run of non-missing values as its own trace. The values are not modified; they are only split at the missing points. Giving the traces the same `legendgroup` and showing only the first one in the legend keeps them behaving like a single series.
137+
138+
```python
139+
import math
140+
import plotly.graph_objects as go
141+
142+
x = list(range(1, 11))
143+
y = [2, 3, 4, None, None, 5, 6, 4, None, 3]
144+
145+
def is_missing(value):
146+
return value is None or (isinstance(value, float) and math.isnan(value))
147+
148+
# Split (x, y) into runs of consecutive non-missing values
149+
segments, current = [], []
150+
for xi, yi in zip(x, y):
151+
if is_missing(yi):
152+
if current:
153+
segments.append(current)
154+
current = []
155+
else:
156+
current.append((xi, yi))
157+
if current:
158+
segments.append(current)
159+
160+
fig = go.Figure()
161+
for i, segment in enumerate(segments):
162+
seg_x, seg_y = zip(*segment)
163+
fig.add_trace(go.Scatter(
164+
x=seg_x, y=seg_y,
165+
fill='tozeroy',
166+
mode='lines+markers',
167+
line_color='royalblue',
168+
name='measurement',
169+
legendgroup='measurement',
170+
showlegend=(i == 0),
171+
))
172+
173+
fig.show()
174+
```
175+
132176
#### Gradient Fill
133177

134178
*New in 5.20*

0 commit comments

Comments
 (0)