Skip to content

Commit d6373f4

Browse files
jonasvddyzhaoinuw
andauthored
Fix: Add xaxis.range[0]/[1] If Not Exist (#342)
* Fix: Add xaxis.range[0]/[1] If Not Exist #335 * 🔍 adding test + some more docs --------- Co-authored-by: yzhaoinuw <[email protected]>
1 parent 0ef968a commit d6373f4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,25 @@ def _construct_update_data(
12991299
# 1. Base case - there is an x-range specified in the front-end
13001300
start_matches = self._re_matches(re.compile(r"xaxis\d*.range\[0]"), cl_k)
13011301
stop_matches = self._re_matches(re.compile(r"xaxis\d*.range\[1]"), cl_k)
1302+
1303+
# related issue: https://github.com/predict-idlab/plotly-resampler/pull/336
1304+
# When the user sets x range via update_xaxes and the number of points in
1305+
# data exceeds the default_n_shown_samples, then after resetting the axes
1306+
# the relayout may only have "xaxis.range", instead of "xaxis.range[0]" and
1307+
# "xaxis.range[1]". If this happens, we need to manually add "xaxis.range[0]"
1308+
# and "xaxis.range[1]", otherwise resetting axes wouldn't work.
1309+
if not (start_matches and stop_matches):
1310+
range_matches = self._re_matches(re.compile(r"xaxis\d*.range"), cl_k)
1311+
for range_match in range_matches:
1312+
x_range = relayout_data[range_match]
1313+
start, stop = x_range
1314+
start_match = range_match + "[0]"
1315+
stop_match = range_match + "[1]"
1316+
relayout_data[start_match] = start
1317+
relayout_data[stop_match] = stop
1318+
start_matches.append(start_match)
1319+
stop_matches.append(stop_match)
1320+
del x_range, start, stop, start_match, stop_match
13021321
if start_matches and stop_matches: # when both are not empty
13031322
for t_start_key, t_stop_key in zip(start_matches, stop_matches):
13041323
# Check if the xaxis<NUMB> part of xaxis<NUMB>.[0-1] matches

tests/test_figure_resampler.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,3 +1984,43 @@ def test_hf_marker_size_plotly_args():
19841984
(np.abs(update_trace["y"]) / np.max(np.abs(y))),
19851985
rtol=1e-3,
19861986
)
1987+
1988+
1989+
@pytest.mark.parametrize("shared_xaxes", [False, True])
1990+
def test_manual_range_def(shared_xaxes):
1991+
# related issue: https://github.com/predict-idlab/plotly-resampler/pull/336
1992+
time = 1000
1993+
N = 4001 # number of points in the subplot exceeds default_n_shown_samples
1994+
x = np.linspace(0.0, time, N, endpoint=False)
1995+
y = np.cos(1 / 2 * np.pi + 2 / 50 * np.pi * x)
1996+
1997+
# Create a subplot with two rows (non shared xaxes)
1998+
fig = FigureResampler(make_subplots(rows=2, cols=1, shared_xaxes=shared_xaxes))
1999+
fig.add_trace(
2000+
go.Scattergl(line=dict(width=1), marker=dict(size=2, color="blue")),
2001+
hf_x=x,
2002+
hf_y=y,
2003+
row=1,
2004+
col=1,
2005+
)
2006+
fig.add_trace(go.Scattergl(), hf_x=x, hf_y=-y, row=2, col=1)
2007+
2008+
fig.update_xaxes(
2009+
range=[0, np.ceil(x[-1])], # set the x range of the subplot
2010+
row=1,
2011+
col=1,
2012+
)
2013+
2014+
fig.update_xaxes(
2015+
range=[0, np.ceil(x[-1])], # set the x range of the subplot
2016+
row=2,
2017+
col=1,
2018+
)
2019+
2020+
# Before this fix, a noUpdate was returned
2021+
ud = fig._construct_update_data({"xaxis.range": [0, 10]})
2022+
assert isinstance(ud, list) and len(ud) == 2
2023+
ud = fig._construct_update_data({"xaxis2.range": [0, 10]})
2024+
assert isinstance(ud, list) and len(ud) == 2
2025+
ud = fig._construct_update_data({"xaxis2.range": [2, 10], "xaxis.range": [5, 100]})
2026+
assert isinstance(ud, list) and len(ud) == 3

0 commit comments

Comments
 (0)