Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 521
clarkmaio Avatar asked Oct 17 '19 14:10

clarkmaio


People also ask

What is the difference between Plotly and Plotly Express?

The plotly. express module (usually imported as px ) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.

How many points can Plotly handle?

Short answer is about 250,000 x-y points.

Does Plotly work with pandas?

The Plotly backend supports the following kind s of Pandas plots: scatter , line , area , bar , barh , hist and box , via the call pattern df.

Does Plotly work with NumPy?

It is possible to mix DataFrame columns, NumPy arrays and lists as arguments.


1 Answers

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

like image 99
nicolaskruchten Avatar answered Oct 20 '22 13:10

nicolaskruchten