Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TST] Added test_arrow in test_datetime.py #27372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions lib/matplotlib/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,61 @@ def test_annotate(self):
fig, ax = plt.subplots()
ax.annotate(...)

@pytest.mark.xfail(reason="Test for arrow not written yet")
@mpl.style.context("default")
def test_arrow(self):
fig, ax = plt.subplots()
ax.arrow(...)
mpl.rcParams["date.converter"] = 'concise'
fig, (ax1, ax2, ax3) = plt.subplots(3, 1,
constrained_layout=True,
figsize=(10, 12))

start_date = datetime.datetime(2023, 1, 1)
dates = [start_date + datetime.timedelta(days=i) for i in range(31)]
y_values = range(31)

ax1.plot(dates, y_values, marker='o')
ax1.arrow(x=dates[10],
y=10,
dx=10,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the d[xy] values that are used on date-based axes to be timedelta rather than raw numbers (and in particular if it doesn't work that is something we should look at)

Copy link
Author

@samchan2022 samchan2022 Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review. Unfortunately, currently the function ax.arrow expects the dx, dy to be in the axis units only. We have discovered another issue while implementing datetime test.
Looks like this PR would be blocked meanwhile.

I have tested a minimal change here to get the timedelta working when dx=datetime.timedelta(days=10) with the following patch.
New unitless d[xy] are computed before passing down to the function mpatches.FancyArrow. The drawback is that we now need to import datetime object dependence into the lib/matplotlib/axes/_axes.py. Would be appreciated if you would suggest a better approach to implement the d[xy] timedelta support.

diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py
index 2136ecb2eb..780e5048e0 100644
--- a/lib/matplotlib/axes/_axes.py
+++ b/lib/matplotlib/axes/_axes.py
@@ -3,6 +3,7 @@ import itertools
 import logging
 import math
 from numbers import Integral, Number, Real
+import datetime
 
 import numpy as np
 from numpy import ma
@@ -5218,13 +5219,22 @@ default: :rc:`scatter.edgecolors`
         ...             arrowprops=dict(arrowstyle="->"))
 
         """
+        old_x=x
+        old_y=y
         # Strip away units for the underlying patch since units
         # do not make sense to most patch-like code
         x = self.convert_xunits(x)
         y = self.convert_yunits(y)
-        dx = self.convert_xunits(dx)
-        dy = self.convert_yunits(dy)
-
+        # if dx is timedelta, compute the unitless dx after convert
+        if isinstance(dx, datetime.timedelta):
+            dx = self.convert_xunits( old_x + dx ) - x
+        else:
+            dx = self.convert_xunits(dx)
+        # if dy is timedelta, compute the unitless dy after convert
+        if isinstance(dy, datetime.timedelta):
+            dy = self.convert_xunits( old_y + dy ) - y
+        else:
+            dy = self.convert_yunits(dy)
         a = mpatches.FancyArrow(x, y, dx, dy, **kwargs)
         self.add_patch(a)
         self._request_autoscale_view()

dy=10,
head_width=1,
head_length=1,
linewidth=3,
facecolor='red',
edgecolor='red',
transform=ax1.transData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specifying the transform as transData is not necessary, as that is the default.

ax1.set_title('Datetime vs. Number')
ax1.set_xlabel('Date')
ax1.set_ylabel('Number')

ax2.plot(y_values, dates, marker='o')
ax2.arrow(x=10,
y=dates[10],
dx=10,
dy=10,
head_width=1,
head_length=1,
linewidth=3,
facecolor='red',
edgecolor='red',
transform=ax2.transData)
ax2.set_title('Number vs. Datetime')
ax2.set_xlabel('Number')
ax2.set_ylabel('Date')

ax3.plot(dates, dates, marker='o')
ax3.arrow(x=dates[10],
y=dates[10],
dx=10,
dy=10,
head_width=1,
head_length=1,
linewidth=3,
facecolor='red',
edgecolor='red',
transform=ax3.transData)
ax3.set_title('Datetime vs. Datetime')
ax3.set_xlabel('Date')
ax3.set_ylabel('Date')

@mpl.style.context("default")
def test_axhline(self):
Expand Down