Skip to content

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

@Atcold

Description

@Atcold

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions