Skip to content

Commit f785746

Browse files
authored
Remove "paper" as default reference when adding annotations/shapes/images (plotly#1888)
* don't default to "paper" when adding annotations/shapes/images don't support setting row/col to "paper", instead use xref/yref="paper"
1 parent cf168ed commit f785746

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/ambv/black
3-
rev: stable
3+
rev: 19.10b0
44
hooks:
55
- id: black
6-
language_version: python
6+
language_version: python

packages/python/plotly/plotly/basedatatypes.py

+4-26
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,11 @@ class is a subclass of both BaseFigure and widgets.DOMWidget.
252252
# Dict from trace indexes to trace edit dicts. These trace edit dicts
253253
# are suitable as `data` elements of Plotly.animate, but not
254254
# the Plotly.update (See `_build_update_params_from_batch`)
255-
#
256-
# type: OrderedDict[int, OrderedDict[str, typ.Any]]
257255
self._batch_trace_edits = OrderedDict()
258256

259257
# ### Batch layout edits ###
260258
# Dict from layout properties to new layout values. This dict is
261259
# directly suitable for use in Plotly.animate and Plotly.update
262-
# type: collections.OrderedDict[str, typ.Any]
263260
self._batch_layout_edits = OrderedDict()
264261

265262
# Animation property validators
@@ -1030,18 +1027,12 @@ def _select_annotations_like(
10301027

10311028
for obj in self.layout[prop]:
10321029
# Filter by row
1033-
if col is not None:
1034-
if col == "paper" and obj.xref != "paper":
1035-
continue
1036-
elif col != "paper" and xref_to_col.get(obj.xref, None) != col:
1037-
continue
1030+
if col is not None and xref_to_col.get(obj.xref, None) != col:
1031+
continue
10381032

10391033
# Filter by col
1040-
if row is not None:
1041-
if row == "paper" and obj.yref != "paper":
1042-
continue
1043-
elif row != "paper" and yref_to_row.get(obj.yref, None) != row:
1044-
continue
1034+
if row is not None and yref_to_row.get(obj.yref, None) != row:
1035+
continue
10451036

10461037
# Filter by secondary y
10471038
if (
@@ -1105,11 +1096,6 @@ def _add_annotation_like(
11051096
xref, yref = xaxis.replace("axis", ""), yaxis.replace("axis", "")
11061097
new_obj.update(xref=xref, yref=yref)
11071098

1108-
if new_obj.xref is None:
1109-
new_obj.xref = "paper"
1110-
if new_obj.yref is None:
1111-
new_obj.yref = "paper"
1112-
11131099
self.layout[prop_plural] += (new_obj,)
11141100

11151101
return self
@@ -2994,38 +2980,32 @@ def __init__(self, plotly_name, **kwargs):
29942980
# ---------------------
29952981
# ### _validators ###
29962982
# A dict from property names to property validators
2997-
# type: Dict[str, BaseValidator]
29982983
self._validators = {}
29992984

30002985
# ### _compound_props ###
30012986
# A dict from compound property names to compound objects
3002-
# type: Dict[str, BasePlotlyType]
30032987
self._compound_props = {}
30042988

30052989
# ### _compound_array_props ###
30062990
# A dict from compound array property names to tuples of compound
30072991
# objects
3008-
# type: Dict[str, Tuple[BasePlotlyType]]
30092992
self._compound_array_props = {}
30102993

30112994
# ### _orphan_props ###
30122995
# A dict of properties for use while object has no parent. When
30132996
# object has a parent, it requests its properties dict from its
30142997
# parent and doesn't use this.
3015-
# type: Dict
30162998
self._orphan_props = {}
30172999

30183000
# ### _parent ###
30193001
# The parent of the object. May be another BasePlotlyType or it may
30203002
# be a BaseFigure (as is the case for the Layout and Trace objects)
3021-
# type: Union[BasePlotlyType, BaseFigure]
30223003
self._parent = None
30233004

30243005
# ### _change_callbacks ###
30253006
# A dict from tuples of child property path tuples to lists
30263007
# of callbacks that should be executed whenever any of these
30273008
# properties is modified
3028-
# type: Dict[Tuple[Tuple[Union[str, int]]], List[Callable]]
30293009
self._change_callbacks = {}
30303010

30313011
def _process_kwargs(self, **kwargs):
@@ -3842,7 +3822,6 @@ def _set_compound_prop(self, prop, val):
38423822
# Import value
38433823
# ------------
38443824
validator = self._validators.get(prop)
3845-
# type: BasePlotlyType
38463825
val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
38473826

38483827
# Save deep copies of current and new states
@@ -3917,7 +3896,6 @@ def _set_array_prop(self, prop, val):
39173896
# Import value
39183897
# ------------
39193898
validator = self._validators.get(prop)
3920-
# type: Tuple[BasePlotlyType]
39213899
val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
39223900

39233901
# Save deep copies of current and new states

packages/python/plotly/plotly/tests/test_core/test_update_objects/test_update_annotations.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def assert_update(
7676
def test_add_annotation_no_grid(self):
7777
# Paper annotation
7878
fig = go.Figure()
79-
fig.add_annotation(text="A")
79+
fig.add_annotation(text="A", yref="paper")
8080
annot = fig.layout.annotations[-1]
8181
self.assertEqual(annot.text, "A")
82-
self.assertEqual(annot.xref, "paper")
82+
self.assertEqual(annot.xref, None)
8383
self.assertEqual(annot.yref, "paper")
8484

8585
# Not valid to add annotation by row/col
@@ -88,10 +88,10 @@ def test_add_annotation_no_grid(self):
8888

8989
def test_add_annotations(self):
9090
# Paper annotation
91-
self.fig.add_annotation(text="A")
91+
self.fig.add_annotation(text="A", yref="paper")
9292
annot = self.fig.layout.annotations[-1]
9393
self.assertEqual(annot.text, "A")
94-
self.assertEqual(annot.xref, "paper")
94+
self.assertEqual(annot.xref, None)
9595
self.assertEqual(annot.yref, "paper")
9696

9797
# (1, 1) annotation
@@ -138,8 +138,10 @@ def test_select_annotations_no_grid(self):
138138

139139
def test_select_annotations(self):
140140
(
141-
self.fig.add_annotation(text="A1", arrowcolor="red")
142-
.add_annotation(text="A2", arrowcolor="blue")
141+
self.fig.add_annotation(
142+
text="A1", arrowcolor="red", xref="paper", yref="paper"
143+
)
144+
.add_annotation(text="A2", arrowcolor="blue", xref="paper", yref="paper")
143145
.add_annotation(text="B", arrowcolor="red", row=1, col=1)
144146
.add_annotation(text="C1", row=1, col=2)
145147
.add_annotation(text="C2", row=1, col=2, secondary_y=True)
@@ -151,13 +153,13 @@ def test_select_annotations(self):
151153
self.assert_selected("annotations", [0, 2], selector=dict(arrowcolor="red"))
152154
self.assert_selected("annotations", [2, 3, 4], row=1)
153155
self.assert_selected("annotations", [2], selector=dict(arrowcolor="red"), row=1)
154-
self.assert_selected("annotations", [0, 1], row="paper", col="paper")
156+
self.assert_selected("annotations", [0, 1], dict(yref="paper", xref="paper"))
155157
self.assert_selected("annotations", [4], secondary_y=True)
156158

157159
def test_select_shapes(self):
158160
(
159-
self.fig.add_shape(opacity=0.1, fillcolor="red")
160-
.add_shape(opacity=0.2, fillcolor="blue")
161+
self.fig.add_shape(opacity=0.1, fillcolor="red", xref="paper", yref="paper")
162+
.add_shape(opacity=0.2, fillcolor="blue", xref="paper", yref="paper")
161163
.add_shape(opacity=0.3, fillcolor="red", row=1, col=1)
162164
.add_shape(opacity=0.4, row=1, col=2)
163165
.add_shape(opacity=0.5, row=1, col=2, secondary_y=True)
@@ -169,13 +171,15 @@ def test_select_shapes(self):
169171
self.assert_selected("shapes", [0, 2], selector=dict(fillcolor="red"))
170172
self.assert_selected("shapes", [2, 3, 4], row=1)
171173
self.assert_selected("shapes", [2], selector=dict(fillcolor="red"), row=1)
172-
self.assert_selected("shapes", [0, 1], row="paper", col="paper")
174+
self.assert_selected("shapes", [0, 1], dict(yref="paper", xref="paper"))
173175
self.assert_selected("shapes", [4], secondary_y=True)
174176

175177
def test_select_images(self):
176178
(
177-
self.fig.add_layout_image(opacity=0.1, source="red")
178-
.add_layout_image(opacity=0.2, source="blue")
179+
self.fig.add_layout_image(
180+
opacity=0.1, source="red", xref="paper", yref="paper"
181+
)
182+
.add_layout_image(opacity=0.2, source="blue", xref="paper", yref="paper")
179183
.add_layout_image(opacity=0.3, source="red", row=1, col=1)
180184
.add_layout_image(opacity=0.4, row=1, col=2)
181185
.add_layout_image(opacity=0.5, row=1, col=2, secondary_y=True)
@@ -187,7 +191,7 @@ def test_select_images(self):
187191
self.assert_selected("images", [0, 2], selector=dict(source="red"))
188192
self.assert_selected("images", [2, 3, 4], row=1)
189193
self.assert_selected("images", [2], selector=dict(source="red"), row=1)
190-
self.assert_selected("images", [0, 1], row="paper", col="paper")
194+
self.assert_selected("images", [0, 1], dict(yref="paper", xref="paper"))
191195
self.assert_selected("images", [4], secondary_y=True)
192196

193197
def test_update_annotations(self):

0 commit comments

Comments
 (0)