Skip to content

Commit d823d7e

Browse files
Convert date x-values of axvspan shapes
1 parent 21d2935 commit d823d7e

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎plotly/matplotlylib/renderer.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ def _draw_axes_span(self, props):
756756
ys = (axes_points if y_in_axes else data_points)[:, 1]
757757
x0, x1 = float(min(xs)), float(max(xs))
758758
y0, y1 = float(min(ys)), float(max(ys))
759+
if self.x_is_mpl_date and not x_in_axes:
760+
x0, x1 = self._convert_x_dates([x0, x1])
759761
axis_suffix = str(self.axis_ct) if self.axis_ct > 1 else ""
760762
xref = "x{0}".format(axis_suffix)
761763
yref = "y{0}".format(axis_suffix)

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,3 +1507,21 @@ def test_blended_unaligned_shapes_do_not_convert_to_spans():
15071507
plotly_fig = tls.mpl_to_plotly(fig)
15081508

15091509
assert len(plotly_fig.layout.shapes) == 0
1510+
1511+
1512+
def test_axvspan_on_date_xaxis_converts_to_dates():
1513+
"""axvspan on a date x axis exports its x extent as date strings."""
1514+
dates = [datetime.datetime(2023, 1, i) for i in range(1, 10)]
1515+
fig, ax = plt.subplots()
1516+
ax.plot(dates, range(len(dates)))
1517+
ax.axvspan(dates[2], dates[5])
1518+
ax.axhspan(2, 4)
1519+
1520+
plotly_fig = tls.mpl_to_plotly(fig)
1521+
1522+
vspan, hspan = plotly_fig.layout.shapes
1523+
assert vspan.xref == "x"
1524+
assert vspan.x0.startswith("2023-01-03")
1525+
assert vspan.x1.startswith("2023-01-06")
1526+
assert hspan.xref == "x domain"
1527+
assert np.allclose([hspan.x0, hspan.x1], [0, 1])

0 commit comments

Comments
 (0)