@@ -109,6 +109,9 @@ def _axis_spanning_shapes_docstr(shape_type):
109
109
x1: float or int
110
110
A number representing the x coordinate of the other side of the rectangle."""
111
111
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.
112
115
row: None, int or 'all'
113
116
Subplot row for shape indexed starting at 1. If 'all', addresses all rows in
114
117
the specified column(s). If both row and col are None, addresses the
@@ -1747,7 +1750,9 @@ def _validate_rows_cols(name, n, vals):
1747
1750
else :
1748
1751
BaseFigure ._raise_invalid_rows_cols (name = name , n = n , invalid = vals )
1749
1752
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
+ ):
1751
1756
"""
1752
1757
Add a trace to the figure
1753
1758
@@ -1787,6 +1792,9 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
1787
1792
make_subplots. See the make_subplots docstring for more info.
1788
1793
* The trace argument is a 2D cartesian trace
1789
1794
(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.
1790
1798
Returns
1791
1799
-------
1792
1800
BaseFigure
@@ -1832,17 +1840,31 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
1832
1840
# TODO add product argument
1833
1841
rows_cols = self ._select_subplot_coordinates (row , col )
1834
1842
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
+ )
1836
1850
return self
1837
1851
1838
1852
return self .add_traces (
1839
1853
data = [trace ],
1840
1854
rows = [row ] if row is not None else None ,
1841
1855
cols = [col ] if col is not None else None ,
1842
1856
secondary_ys = [secondary_y ] if secondary_y is not None else None ,
1857
+ exclude_empty_subplots = exclude_empty_subplots ,
1843
1858
)
1844
1859
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
+ ):
1846
1868
"""
1847
1869
Add traces to the figure
1848
1870
@@ -1879,6 +1901,10 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
1879
1901
List of secondary_y booleans for traces to be added. See the
1880
1902
docstring for `add_trace` for more info.
1881
1903
1904
+ exclude_empty_subplots: boolean
1905
+ If True, the trace will not be added to subplots that don't already
1906
+ have traces.
1907
+
1882
1908
Returns
1883
1909
-------
1884
1910
BaseFigure
@@ -1955,6 +1981,16 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
1955
1981
for trace , row , col , secondary_y in zip (data , rows , cols , secondary_ys ):
1956
1982
self ._set_trace_grid_position (trace , row , col , secondary_y )
1957
1983
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
+
1958
1994
# Make deep copy of trace data (Optimize later if needed)
1959
1995
new_traces_data = [deepcopy (trace ._props ) for trace in data ]
1960
1996
@@ -3748,9 +3784,7 @@ def _process_multiple_axis_spanning_shapes(
3748
3784
lambda x : x is not None ,
3749
3785
[
3750
3786
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 ],
3754
3788
)
3755
3789
for n in range (n_layout_objs_before , n_layout_objs_after )
3756
3790
],
0 commit comments