Skip to content

Commit f810a63

Browse files
Convert date x-values for stairs plots
1 parent ada7af0 commit f810a63

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎plotly/matplotlylib/renderer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ def _draw_step_path(self, props):
811811
showlegend = None
812812
self.plotly_fig.add_trace(
813813
go.Scatter(
814-
x=x,
814+
x=self._convert_x_dates(x),
815815
y=y,
816816
mode="lines",
817817
name=label,

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,22 @@ def test_stairs_converts_to_step_line():
553553
assert tuple(trace.y) == (0.0, 0.0, 1.0, 1.0, 0.0, 0.0)
554554

555555

556+
def test_stairs_date_xaxis():
557+
"""Stairs with date x-values must export date strings,
558+
not raw matplotlib date numbers."""
559+
dates = [
560+
datetime.datetime(2023, 1, 1) + datetime.timedelta(days=i) for i in range(4)
561+
]
562+
fig, ax = plt.subplots()
563+
ax.stairs([0.0, 1.0, 0.0], dates)
564+
565+
plotly_fig = tls.mpl_to_plotly(fig)
566+
567+
assert plotly_fig.layout.xaxis.type == "date"
568+
trace = plotly_fig.data[0]
569+
assert all(isinstance(x, str) for x in trace.x)
570+
571+
556572
def test_stairs_label_used_in_legend():
557573
"""A labeled stairs patch must become a named legend entry."""
558574
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)