Skip to content

Commit 4bcd671

Browse files
authored
V4 Update add_trace, add_traces, and add_{trace} methods to return figure (#1624)
* Removed dead code * Update add_trace, add_traces, and add_{trace} figure methods to return the figure they're called on
1 parent 6afca65 commit 4bcd671

File tree

6 files changed

+124
-127
lines changed

6 files changed

+124
-127
lines changed

Diff for: packages/python/chart-studio/chart_studio/plotly/plotly.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ def _extract_grid_from_fig_like(fig, grid=None, path=''):
17931793
trace_type = trace_dict.get('type', 'scatter')
17941794
if trace_type not in reference_traces:
17951795
reference_traces[trace_type] = reference_fig.add_trace(
1796-
{'type': trace_type})
1796+
{'type': trace_type}).data[-1]
17971797

17981798
reference_trace = reference_traces[trace_type]
17991799
_extract_grid_graph_obj(

Diff for: packages/python/plotly/codegen/datatypes.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,13 @@ def __init__(self""")
265265
f'dict of properties compatible with this constructor '
266266
f'or an instance of {class_name}')]
267267

268-
add_docstring(buffer, node, header=header, prepend_extras=extras)
268+
add_docstring(
269+
buffer,
270+
node,
271+
header=header,
272+
prepend_extras=extras,
273+
return_type=node.name_datatype_class,
274+
)
269275

270276
buffer.write(f"""
271277
super({datatype_class}, self).__init__('{node.name_property}')
@@ -424,7 +430,14 @@ def add_constructor_params(buffer,
424430
):""")
425431

426432

427-
def add_docstring(buffer, node, header, prepend_extras=(), append_extras=()):
433+
def add_docstring(
434+
buffer,
435+
node,
436+
header,
437+
prepend_extras=(),
438+
append_extras=(),
439+
return_type=None,
440+
):
428441
"""
429442
Write docstring for a compound datatype node
430443

@@ -443,6 +456,8 @@ def add_docstring(buffer, node, header, prepend_extras=(), append_extras=()):
443456
append_extras :
444457
List or tuple of propery name / description pairs that should be
445458
included at the end of the docstring
459+
return_type :
460+
The docstring return type
446461
Returns
447462
-------
448463

@@ -511,7 +526,7 @@ def add_docstring(buffer, node, header, prepend_extras=(), append_extras=()):
511526

512527
Returns
513528
-------
514-
{node.name_datatype_class}
529+
{return_type}
515530
\"\"\"""")
516531

517532

Diff for: packages/python/plotly/codegen/figure.py

+7-21
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,6 @@ def __init__(self, data=None, layout=None,
110110
d for d in trace_node.child_datatypes
111111
if d.name_property == 'yaxis'
112112
])
113-
if include_secondary_y:
114-
secondary_y_1 = ', secondary_y=None'
115-
secondary_y_2 = ', secondary_y=secondary_y'
116-
secondary_y_docstring = f"""
117-
secondary_y: boolean or None (default None)
118-
* If True, only select yaxis objects associated with the secondary
119-
y-axis of the subplot.
120-
* If False, only select yaxis objects associated with the primary
121-
y-axis of the subplot.
122-
* If None (the default), do not filter yaxis objects based on
123-
a secondary y-axis condition.
124-
125-
To select yaxis objects by secondary y-axis, the Figure must
126-
have been created using plotly.subplots.make_subplots. See
127-
the docstring for the specs argument to make_subplots for more
128-
info on creating subplots with secondary y-axes."""
129-
else:
130-
secondary_y_1 = ''
131-
secondary_y_2 = ''
132-
secondary_y_docstring = ''
133113

134114
# #### Function signature ####
135115
buffer.write(f"""
@@ -171,7 +151,13 @@ def add_{trace_node.plotly_name}(self""")
171151
""")
172152
)
173153

174-
add_docstring(buffer, trace_node, header, append_extras=doc_extras)
154+
add_docstring(
155+
buffer,
156+
trace_node,
157+
header,
158+
append_extras=doc_extras,
159+
return_type=fig_classname,
160+
)
175161

176162
# #### Function body ####
177163
buffer.write(f"""

Diff for: packages/python/plotly/plotly/basedatatypes.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -1447,12 +1447,12 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
14471447
(scatter, bar, etc.)
14481448
Returns
14491449
-------
1450-
BaseTraceType
1451-
The newly added trace
1450+
BaseFigure
1451+
The Figure that add_trace was called on
14521452

14531453
Examples
14541454
--------
1455-
>>> from plotly import tools
1455+
>>> from plotly import subplots
14561456
>>> import plotly.graph_objs as go
14571457

14581458
Add two Scatter traces to a figure
@@ -1462,11 +1462,7 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
14621462

14631463

14641464
Add two Scatter traces to vertically stacked subplots
1465-
>>> fig = tools.make_subplots(rows=2)
1466-
This is the format of your plot grid:
1467-
[ (1,1) x1,y1 ]
1468-
[ (2,1) x2,y2 ]
1469-
1465+
>>> fig = subplots.make_subplots(rows=2)
14701466
>>> fig.add_trace(go.Scatter(x=[1,2,3], y=[2,1,2]), row=1, col=1)
14711467
>>> fig.add_trace(go.Scatter(x=[1,2,3], y=[2,1,2]), row=2, col=1)
14721468
"""
@@ -1492,7 +1488,7 @@ def add_trace(self, trace, row=None, col=None, secondary_y=None):
14921488
rows=[row] if row is not None else None,
14931489
cols=[col] if col is not None else None,
14941490
secondary_ys=[secondary_y] if secondary_y is not None else None
1495-
)[0]
1491+
)
14961492

14971493
def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
14981494
"""
@@ -1528,12 +1524,12 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
15281524

15291525
Returns
15301526
-------
1531-
tuple[BaseTraceType]
1532-
Tuple of the newly added traces
1527+
BaseFigure
1528+
The Figure that add_traces was called on
15331529

15341530
Examples
15351531
--------
1536-
>>> from plotly import tools
1532+
>>> from plotly import subplots
15371533
>>> import plotly.graph_objs as go
15381534

15391535
Add two Scatter traces to a figure
@@ -1542,11 +1538,7 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
15421538
... go.Scatter(x=[1,2,3], y=[2,1,2])])
15431539

15441540
Add two Scatter traces to vertically stacked subplots
1545-
>>> fig = tools.make_subplots(rows=2)
1546-
This is the format of your plot grid:
1547-
[ (1,1) x1,y1 ]
1548-
[ (2,1) x2,y2 ]
1549-
1541+
>>> fig = subplots.make_subplots(rows=2)
15501542
>>> fig.add_traces([go.Scatter(x=[1,2,3], y=[2,1,2]),
15511543
... go.Scatter(x=[1,2,3], y=[2,1,2])],
15521544
... rows=[1, 2], cols=[1, 1])
@@ -1608,7 +1600,7 @@ def add_traces(self, data, rows=None, cols=None, secondary_ys=None):
16081600
# Update messages
16091601
self._send_addTraces_msg(new_traces_data)
16101602

1611-
return data
1603+
return self
16121604

16131605
# Subplots
16141606
# --------

0 commit comments

Comments
 (0)