clarkmaio
clarkmaio

Reputation: 399

How to DO NOT fill area under NaN in plotly

Ciao all,

I am working with plotly.

I need to draw a curve and fill the area under it. It could be some NaNs appear and it is very important that in that case area is not filled.

Please notice that in my case 0 and NaN are different so that setting NaN to 0 is a solution for the area issue that does not fit in my case.

I tried with the following code:

import plotly.graph_objs as go
from plotly.offline import plot

p = go.Scatter(x=range(0,12),
           y = [1,2,3,np.nan, np.nan, np.nan, np.nan, np.nan, 0,0,0,5],
           fill='tozeroy')
fig = go.Figure([p])
plot(fig, auto_open=True)

but I get a result I do not like. As you can see there are no points corresponding to the NaN, so that nothing will be displays in the hovermode (I like it!) but there is the area.

enter image description here

I'd like to reach this result:

import plotly.graph_objs as go
from plotly.offline import plot

p1 = go.Scatter(x=range(0,3),
           y = [1,2,3],
           fill='tozeroy')
p2 = go.Scatter(x=range(8,12),
           y = [0,0,0,5],
           fill='tozeroy')

fig = go.Figure([p1, p2])
plot(fig, auto_open=True)

enter image description here

(Of course I'd like the plot to have only one color and only one element in the legend.)

Can you help me to reproduce the second plot without splitting the curve in multiple curves?

Thanks

am

Upvotes: 9

Views: 3444

Answers (1)

nicolaskruchten
nicolaskruchten

Reputation: 27370

Edited: I don't believe there is currently a way to do this with areas, although it does work for lines. Here is the open issue: https://github.com/plotly/plotly.js/issues/3244

Upvotes: 2

Related Questions