Skip to content

Commit 24ad4c7

Browse files
committed
added some conditionals to help find bug in subplot shapes
1 parent d0522f5 commit 24ad4c7

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

debug.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import plotly.express as px
2+
import plotly.graph_objects as go
3+
import numpy as np
4+
from plotly import subplots
5+
6+
test_subplots = True
7+
use_OP = False
8+
9+
df = px.data.iris()
10+
if use_OP:
11+
fig = px.scatter(df, x="petal_length", y="petal_width")
12+
fig.add_traces(go.Scatter(y=np.arange(1, 7), mode="lines+markers", yaxis="y2"))
13+
14+
fig.update_layout(
15+
yaxis2=dict(
16+
title="yaxis2 title",
17+
overlaying="y",
18+
side="right",
19+
)
20+
)
21+
else:
22+
trace1 = go.Scatter(x=df["petal_length"], y=df["petal_width"], mode="markers")
23+
trace2 = go.Scatter(
24+
x=[0, 1, 2, 3, 4, 5], y=np.arange(1, 7), mode="lines+markers", yaxis="y2"
25+
)
26+
data = [trace1, trace2]
27+
layout = go.Layout(
28+
yaxis=dict(title="yaxis1"),
29+
yaxis2=dict(title="yaxis2 title", overlaying="y", side="right"),
30+
)
31+
fig = go.Figure(data=data, layout=layout)
32+
if test_subplots:
33+
fig = subplots.make_subplots(rows=1, cols=2, shared_yaxes=False)
34+
35+
fig.add_hline(
36+
y=2,
37+
line_dash="dash",
38+
line_color="Red", # yref='y2'
39+
# secondary_y=True
40+
)
41+
fig.show()

debugtesting.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import plotly.express as px
2+
import plotly.graph_objects as go
3+
import numpy as np
4+
from plotly.subplots import make_subplots
5+
6+
7+
for k, fun, d, fun2, d2 in [
8+
(
9+
"shapes",
10+
go.Figure.add_shape,
11+
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
12+
# add a different type to make the check easier (otherwise we might
13+
# mix up the objects added before and after fun was run)
14+
go.Figure.add_annotation,
15+
dict(x=1, y=2, text="A"),
16+
),
17+
(
18+
"annotations",
19+
go.Figure.add_annotation,
20+
dict(x=1, y=2, text="A"),
21+
go.Figure.add_layout_image,
22+
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
23+
),
24+
(
25+
"images",
26+
go.Figure.add_layout_image,
27+
dict(x=3, y=4, sizex=2, sizey=3, source="test"),
28+
go.Figure.add_shape,
29+
dict(type="rect", x0=1.5, x1=2.5, y0=3.5, y1=4.5),
30+
),
31+
]:
32+
# make a figure where not all the subplots are populated
33+
fig = make_subplots(2, 2)
34+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 1, 2]), row=1, col=1)
35+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, -7]), row=2, col=2)
36+
fun2(fig, d2, row=1, col=2)
37+
# add a thing to all subplots but make sure it only goes on the
38+
# plots without data or layout objects
39+
fun(fig, d, row="all", col="all", exclude_empty_subplots="anything_truthy")
40+
assert len(fig.layout[k]) == 3
41+
assert fig.layout[k][0]["xref"] == "x" and fig.layout[k][0]["yref"] == "y"
42+
assert fig.layout[k][1]["xref"] == "x2" and fig.layout[k][1]["yref"] == "y2"
43+
assert fig.layout[k][2]["xref"] == "x4" and fig.layout[k][2]["yref"] == "y4"

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,7 @@ def _add_annotation_like(
15671567
prop_singular=prop_singular, r=row, c=col
15681568
)
15691569
)
1570+
#'''
15701571
# If the new_object was created with an xref specified, the specified xref should be used otherwise assign the xref from the layout_keys
15711572
if new_obj.xref == None:
15721573
if secondary_y:
@@ -1585,6 +1586,14 @@ def _add_annotation_like(
15851586
yref = yaxis.replace("axis", "")
15861587
else:
15871588
yref = new_obj.yref
1589+
#'''
1590+
"""
1591+
if secondary_y:
1592+
xaxis, yaxis = refs[1].layout_keys
1593+
else:
1594+
xaxis, yaxis = refs[0].layout_keys
1595+
xref, yref = xaxis.replace("axis", ""), yaxis.replace("axis", "")
1596+
"""
15881597
# if exclude_empty_subplots is True, check to see if subplot is
15891598
# empty and return if it is
15901599
if exclude_empty_subplots and (
@@ -4063,10 +4072,12 @@ def _process_multiple_axis_spanning_shapes(
40634072
# however, in the case of a single plot, xref and yref MAY not be
40644073
# specified, IF they are not specified we specify them here so the following routines can work
40654074
# (they need to append " domain" to xref or yref)
4066-
if self.layout[layout_obj][-1].xref is None:
4075+
if self.layout[layout_obj][-1].xref == None:
40674076
self.layout[layout_obj][-1].update(xref="x")
4068-
if self.layout[layout_obj][-1].yref is None:
4077+
# self.layout[layout_obj][-1].update(xref="x")
4078+
if self.layout[layout_obj][-1].yref == None:
40694079
self.layout[layout_obj][-1].update(yref="y")
4080+
# self.layout[layout_obj][-1].update(yref='y')
40704081
new_layout_objs = tuple(
40714082
filter(
40724083
lambda x: x is not None,

0 commit comments

Comments
 (0)