Skip to content
Draft

Vector #22435

Changes from 6 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
39 changes: 39 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6777,6 +6777,45 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
else "list[Polygon]")
return tops, bins, cbook.silent_list(patch_type, patches)

def vector(self, dx, dy, x=0., y=0., **kwargs):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def vector(self, dx, dy, x=0., y=0., **kwargs):
def vector(self, x, y, dx, dy, **kwargs):
  • We want to keep the API similar to arrow()
  • While from a mathemetical point of view, you can have a direction vector only specifying dx, dy; but we need to plot it somewhere and there is no reasonable default. I don't think there is a real use case where you are not interested how the arrow is positioned relative to other plot elements.
  • If you have all four parameters x, y, dx, dy is the natural order.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This may be an peculiarity of my field, but crystallographers frequently plot arrows in the complex plane that are by default rooted at zero. As evidence that I am not alone, have a look at an image search for Argand diagram. From my perspective, x=y=0 is a sensible default. I can understand how this might not be universal.

I find vector(tail_x, tail_y, head_x, head_y, ...) is also a natural four-parameter representation. Is that one the table?

"""
Plot an arrow indicating a vector.

Parameters
----------
dx : float
The x-component of the vector
dy : float
The y-component of the vector
x : float (optional)
The x-coordinate of the vector's base if nonzero.
y : float (optional)
The y-coordinate of the vector's base if nonzero.

Other Parameters
----------------
**kwargs
`~matplotlib.patches.FancyArrowPatch` properties
"""
posA = (x, y)
posB = (x + dx, y + dy)

color = kwargs.pop("color", None)
if color is None:
color = self._get_lines.get_next_color()
vect = mpatches.FancyArrowPatch(posA, posB, color=color, **kwargs)
ms = vect._mutation_scale
stylekw = {
"head_length": kwargs.get("head_length", 12) / ms,
"head_width": kwargs.get("head_width", 12) / ms,
"tail_width": kwargs.get("tail_width", 4) / ms,
}
Comment on lines +6812 to +6817
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For context this numbers are based on how annotate styles it's arrows.

vect.set_arrowstyle(kwargs.get("arrowstyle", "simple"), **stylekw)
self.add_patch(vect)
self.update_datalim([posA, posB])
self._request_autoscale_view()
return vect

@_preprocess_data()
def stairs(self, values, edges=None, *,
orientation='vertical', baseline=0, fill=False, **kwargs):
Expand Down