@@ -109,6 +109,9 @@ def _axis_spanning_shapes_docstr(shape_type):
109109x1: float or int 
110110    A number representing the x coordinate of the other side of the rectangle.""" 
111111    docstr  +=  """ 
112+ exclude_empty_subplots: Boolean 
113+     If True (default) do not place the shape on subplots that have no data 
114+     plotted on them. 
112115row: None, int or 'all' 
113116    Subplot row for shape indexed starting at 1. If 'all', addresses all rows in 
114117    the specified column(s). If both row and col are None, addresses the 
@@ -1747,7 +1750,9 @@ def _validate_rows_cols(name, n, vals):
17471750        else :
17481751            BaseFigure ._raise_invalid_rows_cols (name = name , n = n , invalid = vals )
17491752
1750-     def  add_trace (self , trace , row = None , col = None , secondary_y = None ):
1753+     def  add_trace (
1754+         self , trace , row = None , col = None , secondary_y = None , exclude_empty_subplots = False 
1755+     ):
17511756        """ 
17521757        Add a trace to the figure 
17531758
@@ -1787,6 +1792,9 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
17871792                make_subplots. See the make_subplots docstring for more info. 
17881793              * The trace argument is a 2D cartesian trace 
17891794                (scatter, bar, etc.) 
1795+         exclude_empty_subplots: boolean 
1796+             If True, the trace will not be added to subplots that don't already 
1797+             have traces. 
17901798        Returns 
17911799        ------- 
17921800        BaseFigure 
@@ -1832,17 +1840,31 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
18321840            # TODO add product argument 
18331841            rows_cols  =  self ._select_subplot_coordinates (row , col )
18341842            for  r , c  in  rows_cols :
1835-                 self .add_trace (trace , row = r , col = c , secondary_y = secondary_y )
1843+                 self .add_trace (
1844+                     trace ,
1845+                     row = r ,
1846+                     col = c ,
1847+                     secondary_y = secondary_y ,
1848+                     exclude_empty_subplots = exclude_empty_subplots ,
1849+                 )
18361850            return  self 
18371851
18381852        return  self .add_traces (
18391853            data = [trace ],
18401854            rows = [row ] if  row  is  not   None  else  None ,
18411855            cols = [col ] if  col  is  not   None  else  None ,
18421856            secondary_ys = [secondary_y ] if  secondary_y  is  not   None  else  None ,
1857+             exclude_empty_subplots = exclude_empty_subplots ,
18431858        )
18441859
1845-     def  add_traces (self , data , rows = None , cols = None , secondary_ys = None ):
1860+     def  add_traces (
1861+         self ,
1862+         data ,
1863+         rows = None ,
1864+         cols = None ,
1865+         secondary_ys = None ,
1866+         exclude_empty_subplots = False ,
1867+     ):
18461868        """ 
18471869        Add traces to the figure 
18481870
@@ -1879,6 +1901,10 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
18791901            List of secondary_y booleans for traces to be added. See the 
18801902            docstring for `add_trace` for more info. 
18811903
1904+         exclude_empty_subplots: boolean 
1905+             If True, the trace will not be added to subplots that don't already 
1906+             have traces. 
1907+ 
18821908        Returns 
18831909        ------- 
18841910        BaseFigure 
@@ -1955,6 +1981,16 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
19551981            for  trace , row , col , secondary_y  in  zip (data , rows , cols , secondary_ys ):
19561982                self ._set_trace_grid_position (trace , row , col , secondary_y )
19571983
1984+         if  exclude_empty_subplots :
1985+             data  =  list (
1986+                 filter (
1987+                     lambda  trace : self ._subplot_contains_trace (
1988+                         trace ["xaxis" ], trace ["yaxis" ]
1989+                     ),
1990+                     data ,
1991+                 )
1992+             )
1993+ 
19581994        # Make deep copy of trace data (Optimize later if needed) 
19591995        new_traces_data  =  [deepcopy (trace ._props ) for  trace  in  data ]
19601996
@@ -3748,9 +3784,7 @@ def _process_multiple_axis_spanning_shapes(
37483784                    lambda  x : x  is  not   None ,
37493785                    [
37503786                        self ._make_axis_spanning_layout_object (
3751-                             direction ,
3752-                             self .layout [layout_obj ][n ],
3753-                             none_if_no_trace = exclude_empty_subplots ,
3787+                             direction , self .layout [layout_obj ][n ],
37543788                        )
37553789                        for  n  in  range (n_layout_objs_before , n_layout_objs_after )
37563790                    ],
0 commit comments