Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d350bc

Browse files
go.Figure now has set_subplots
Calling `fig.set_subplots(2,3)` works just like fig=make_subplots(2,3). Also accepts the same keywords arguments as make_subplots. Fails if fig already has subplots. Tests still need to be added.
1 parent 63f20ee commit 9d350bc

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .optional_imports import get_module
1515

1616
from . import shapeannotation
17+
from . import subplots
1718

1819
# Create Undefined sentinel value
1920
# - Setting a property to None removes any existing value
@@ -3926,6 +3927,22 @@ def _subplot_not_empty(self, xref, yref, selector="all"):
39263927
)
39273928
return ret
39283929

3930+
def set_subplots(self, rows=None, cols=None, **make_subplots_args):
3931+
"""
3932+
Add subplots to this figure. If the figure already contains subplots,
3933+
then this throws an error. Accepts any keyword arguments that
3934+
plotly.subplots.make_subplots accepts.
3935+
"""
3936+
# rows, cols provided so that this can be called like
3937+
# fig.set_subplots(2,3), say
3938+
if rows is not None:
3939+
make_subplots_args["rows"] = rows
3940+
if cols is not None:
3941+
make_subplots_args["cols"] = cols
3942+
if self._has_subplots():
3943+
raise ValueError("This figure already has subplots.")
3944+
return subplots.make_subplots(figure=self, **make_subplots_args)
3945+
39293946

39303947
class BasePlotlyType(object):
39313948
"""

packages/python/plotly/plotly/subplots.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def make_subplots(
6060
row_titles=None,
6161
x_title=None,
6262
y_title=None,
63+
figure=None,
6364
**kwargs
6465
):
6566
"""
@@ -226,7 +227,15 @@ def make_subplots(
226227
227228
y_title: str or None (default None)
228229
Title to place to the left of the left column of subplots,
229-
centered vertically
230+
centered vertically
231+
232+
figure: go.Figure or None (default None)
233+
If None, a new go.Figure instance will be created and its axes will be
234+
populated with those corresponding to the requested subplot geometry and
235+
this new figure will be returned.
236+
If a go.Figure instance, the axes will be added to the
237+
layout of this figure and this figure will be returned. If the figure
238+
already contains axes, they will be overwritten.
230239
231240
Examples
232241
--------
@@ -809,13 +818,15 @@ def _checks(item, defaults):
809818
print(grid_str)
810819

811820
# Build resulting figure
812-
fig = go.Figure(layout=layout)
821+
if figure is None:
822+
figure = go.Figure()
823+
figure.update_layout(layout)
813824

814825
# Attach subplot grid info to the figure
815-
fig.__dict__["_grid_ref"] = grid_ref
816-
fig.__dict__["_grid_str"] = grid_str
826+
figure.__dict__["_grid_ref"] = grid_ref
827+
figure.__dict__["_grid_str"] = grid_str
817828

818-
return fig
829+
return figure
819830

820831

821832
def _configure_shared_axes(layout, grid_ref, specs, x_or_y, shared, row_dir):

0 commit comments

Comments
 (0)