FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: stem complaining about PyTorch's Tensor · Issue #30216 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: stem complaining about PyTorch's Tensor #30216

Closed

Description

Bug summary

stem breaks when I specify the horizontal components with a PyTorch Tensor.

Code for reproduction

import torch
from matplotlib import pyplot as plt

x = torch.arange(10.)
y = (x * .2 * torch.pi).sin()
plt.plot(x, y)  # works
plt.stem(x, y)  # breaks
plt.stem(x.numpy(), y)  # works

Actual outcome

TypeError: min() received an invalid combination of arguments - got (out=NoneType, axis=NoneType, ), but expected one of:
 * ()
 * (Tensor other)
 * (int dim, bool keepdim = False)
      didn't match because some of the keywords were incorrect: out, axis
 * (name dim, bool keepdim = False)
      didn't match because some of the keywords were incorrect: out, axis
Full traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[50], line 7
      5 y = (x * .2 * torch.pi).sin()
      6 plt.plot(x, y)  # works
----> 7 plt.stem(x, y)  # breaks
      8 plt.stem(x.numpy(), y)  # works

File [/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/pyplot.py:4085](http://localhost:8888/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/pyplot.py#line=4084), in stem(linefmt, markerfmt, basefmt, bottom, label, orientation, data, *args)
   4074 @_copy_docstring_and_deprecators(Axes.stem)
   4075 def stem(
   4076     *args: ArrayLike | str,
   (...)   4083     data=None,
   4084 ) -> StemContainer:
-> 4085     return gca().stem(
   4086         *args,
   4087         linefmt=linefmt,
   4088         markerfmt=markerfmt,
   4089         basefmt=basefmt,
   4090         bottom=bottom,
   4091         label=label,
   4092         orientation=orientation,
   4093         **({"data": data} if data is not None else {}),
   4094     )

File [/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/__init__.py:1521](http://localhost:8888/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/__init__.py#line=1520), in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1518 @functools.wraps(func)
   1519 def inner(ax, *args, data=None, **kwargs):
   1520     if data is None:
-> 1521         return func(
   1522             ax,
   1523             *map(cbook.sanitize_sequence, args),
   1524             **{k: cbook.sanitize_sequence(v) for k, v in kwargs.items()})
   1526     bound = new_sig.bind(ax, *args, **kwargs)
   1527     auto_label = (bound.arguments.get(label_namer)
   1528                   or bound.kwargs.get(label_namer))

File [/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/axes/_axes.py:3216](http://localhost:8888/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/matplotlib/axes/_axes.py#line=3215), in Axes.stem(self, linefmt, markerfmt, basefmt, bottom, label, orientation, *args)
   3214     marker_x = locs
   3215     marker_y = heads
-> 3216     baseline_x = [np.min(locs), np.max(locs)]
   3217     baseline_y = [bottom, bottom]
   3219 markerline, = self.plot(marker_x, marker_y,
   3220                         color=markercolor, linestyle=markerstyle,
   3221                         marker=markermarker, label="_nolegend_")

File [/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/numpy/_core/fromnumeric.py:3301](http://localhost:8888/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/numpy/_core/fromnumeric.py#line=3300), in min(a, axis, out, keepdims, initial, where)
   3189 @array_function_dispatch(_min_dispatcher)
   3190 def min(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
   3191         where=np._NoValue):
   3192     """
   3193     Return the minimum of an array or minimum along an axis.
   3194 
   (...)   3299     6
   3300     """
-> 3301     return _wrapreduction(a, np.minimum, 'min', axis, None, out,
   3302                           keepdims=keepdims, initial=initial, where=where)

File [/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/numpy/_core/fromnumeric.py:84](http://localhost:8888/opt/homebrew/Caskroom/miniconda/base/envs/book/lib/python3.13/site-packages/numpy/_core/fromnumeric.py#line=83), in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     82             return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
     83         else:
---> 84             return reduction(axis=axis, out=out, **passkwargs)
     86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: min() received an invalid combination of arguments - got (out=NoneType, axis=NoneType, ), but expected one of:
 * ()
 * (Tensor other)
 * (int dim, bool keepdim = False)
      didn't match because some of the keywords were incorrect: out, axis
 * (name dim, bool keepdim = False)
      didn't match because some of the keywords were incorrect: out, axis

Expected outcome

Most of the time, matplotlib is capable of plotting tensors without issues.

Additional information

PyTorch version: 2.7.0

Operating system

No response

Matplotlib Version

3.10.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.13.5

Jupyter version

4.4.3

Installation

conda

Activity

  1. QuLogic commented on Jun 26, 2025

    Member

    Always post the full traceback.

  2. Atcold commented on Jun 26, 2025

    Author

    @QuLogic, just added it. Thanks for checking in.

  3. QuLogic commented on Jul 1, 2025

    Member

    It's a bit unfortunate that torch Tensors don't properly implement the NumPy API.

    BTW, does len(torch.arange(10.)) work correctly?

  4. added a commit that references this issue on Jul 1, 2025
    9b6c617
  5. QuLogic commented on Jul 1, 2025

    Member

    Please try the linked PR.

  6. added a commit that references this issue on Jul 4, 2025
    7572823
  7. added this to the v3.10.4 milestone on Jul 7, 2025
  8. Atcold commented on Jul 11, 2025

    Author

    len(torch.arange(10.))

    Uh… yes?

    In [1]: import torch
    In [2]: len(torch.arange(10.))
    Out[2]: 10

    By 'try the PR' you're suggesting to install matplotlib from source, yes?

  9. QuLogic commented on Jul 12, 2025

    Member

    Sure, or you could just apply the patch; it won't require rebuilding. Or you can pull the wheels from https://github.com/matplotlib/matplotlib/actions/runs/16100956663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL