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

Conversation

samchan2022
Copy link

PR summary

This PR adds a smoke test for Axes.arrow in lib/matplotlib/tests/test_datetime.py. This PR closes one of the items listed in the issue #26864.
Smoke test Plot:
mpl_arrow

PR checklist

Copy link
Member

@ksunden ksunden left a comment

Choose a reason for hiding this comment

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

mostly some things to think about, I would like to know about the d[xy] values in particular.

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.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()

@saifahmed24
Copy link

Overview

Hey @samchan2022 and @dstansby, I have joined the #incubator channel and would like to be assigned this issue. I was wondering if I could have a little bit more direction as this is my first time contributing to matplotlib. I was reading up on the documentation about matplotlib.axes.Axes.arrow and saw that Axes.arrow(x, y, dx, dy, **kwargs) is how we draw an arrow from (x, y) to (x+dx, y+dy).

Understanding The Issue

After spending some time trying to reading through the documentation provided, I believe that this issue is asking us to modify the following part of matplotlob/lib/matplotlib/tests/test_datetime.py:

    @pytest.mark.xfail(reason="Test for arrow not written yet")
    @mpl.style.context("default")
    def test_arrow(self):
        fig, ax = plt.subplots()
        ax.arrow(...)

We can do this by filling in the test_arrow function to create a "Smoke Test".

[Basic] Proposed Solution

We can start by removing the @pytest.mark.xfail(reason="Test for arrow not written yet") line because we don't want this to be marked as failed (upon completion of a correct test case, we don't expect it to fail). We can also plot a basic arrow using the Axes.arrow(x, y, dx, dy, **kwargs) command.

    @mpl.style.context("default")
    def test_arrow(self):
        fig, ax = plt.subplots()
        Axes.arrow(0, 0, 1, 1) # draw an arrow from (0,0) to (1,1) 

We may also want to test **kwargs like:

image
image

Next Steps / Questions

I would appreciate it if I was assigned this issue and I would love more guidance. I'll be sure to send messages in the #incubator channel. I had a few questions as well:

  • Should I be creating the plots shown in the PR Summary and then adding arrows to that or adding arrows to any plot?
  • In the main issue tab ([TST]: increase unit test coverage  #26864), it says that "The most common "unit-full" data is likely datetime, thus we are going to use datetime and timedelta values as the vehicle", are these values we should pull from somewhere or any hard-coded values we write in for this specific test?
  • Is there a template I can follow for creating the test case or related examples I can draw inspiration from?
    • Also, in the main issue tab ([TST]: increase unit test coverage  #26864), it says we will use these datetime values "to exhaustively test which of the methods correctly handle units." Is there a way to determine (metric of somekind) if a smoke test is has "exhaustively tested" the methods?

Please feel free to ask me any questions or provide more explanation if necessary. I look forward to becoming a contributor to matplotlib!😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

4 participants