Skip to content

Commit ab96ba4

Browse files
Convert date x-values per stairs segment
1 parent e0cd136 commit ab96ba4

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎plotly/matplotlylib/renderer.py‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,15 +795,27 @@ def _draw_step_path(self, props):
795795
self.msg += " Step path is not in data coordinates, not drawing\n"
796796
return
797797
style = props["style"]
798+
segments = []
799+
segment_x = []
800+
segment_y = []
801+
for (x0, y0), code in zip(props["data"], props["pathcodes"]):
802+
if code == "M" and segment_x:
803+
segments.append((segment_x, segment_y))
804+
segment_x = []
805+
segment_y = []
806+
if not segment_x or x0 != segment_x[-1] or y0 != segment_y[-1]:
807+
segment_x.append(x0)
808+
segment_y.append(y0)
809+
if segment_x:
810+
segments.append((segment_x, segment_y))
798811
x = []
799812
y = []
800-
for (x0, y0), code in zip(props["data"], props["pathcodes"]):
801-
if code == "M" and x:
813+
for sub_x, sub_y in segments:
814+
if x:
802815
x.append(None)
803816
y.append(None)
804-
if not x or x0 != x[-1] or y0 != y[-1]:
805-
x.append(x0)
806-
y.append(y0)
817+
x.extend(self._convert_x_dates(sub_x))
818+
y.extend(sub_y)
807819
if len(x) < 2:
808820
self.msg += " Step path has fewer than 2 points, not drawing\n"
809821
return

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,21 @@ def test_stairs_nan_values_split_into_disjoint_steps():
594594
assert tuple(trace.y) == (0.2, 1.0, 1.0, 0.2, None, 0.2, 0.5, 0.5, 0.2)
595595

596596

597+
def test_stairs_date_xaxis_with_nan_values():
598+
"""NaN-separated stairs on a date axis must export date strings."""
599+
dates = [
600+
datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(4)
601+
]
602+
fig, ax = plt.subplots()
603+
ax.stairs([1.0, np.nan, 0.5], dates)
604+
605+
plotly_fig = tls.mpl_to_plotly(fig)
606+
607+
trace = plotly_fig.data[0]
608+
assert None in trace.x
609+
assert all(isinstance(x, str) or x is None for x in trace.x)
610+
611+
597612
def test_stairs_label_used_in_legend():
598613
"""A labeled stairs patch must become a named legend entry."""
599614
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)