Skip to content

Can't plot point size when using sympy return types: AttributeError: 'Integer' object has no attribute 'sqrt' #13106

Closed

Description

@Kuhron

Bug report

Bug summary

This happens because some sympy functions return a "One" or "Integer" object. So it is not matplotlib's fault that they do this instead of a normal int, but it took me a while to figure out this error. Including a cast to float would fix it.

More confusingly, somehow it looks like the matplotlib code thinks that "np" is the number I passed as the point size, since it is checking whether that has a "sqrt" attribute.

Code for reproduction

Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'3.0.2'
>>> import matplotlib.pyplot as plt
>>> import sympy
>>> size = sympy.divisor_count(48)
>>> size
10
>>> plt.scatter(0, 0, s=size)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wesley/.local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2864, in scatter
    is not None else {}), **kwargs)
  File "/home/wesley/.local/lib/python3.5/site-packages/matplotlib/__init__.py", line 1810, in inner
    return func(ax, *args, **kwargs)
  File "/home/wesley/.local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 4297, in scatter
    alpha=alpha
  File "/home/wesley/.local/lib/python3.5/site-packages/matplotlib/collections.py", line 901, in __init__
    self.set_sizes(sizes)
  File "/home/wesley/.local/lib/python3.5/site-packages/matplotlib/collections.py", line 874, in set_sizes
    scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
AttributeError: 'Integer' object has no attribute 'sqrt'
>>>
>>> # a simple cast to float fixes it.
>>> plt.scatter(0, 0, s=float(size))
<matplotlib.collections.PathCollection object at 0x7fc15937a4a8>
>>>

Actual outcome

# If applicable, paste the console output here
#
#

Expected outcome

Matplotlib version

  • Operating system: Ubuntu 16.04
  • Matplotlib version: 3.0.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.5.2
  • Jupyter version (if applicable):
  • Other libraries: sympy 1.3

Activity

  1. timhoffm commented on Jan 5, 2019

    @timhoffm
    Member

    Actually, that's rather an issue between numpy and sympy. numpy does not consider a sympy Integer to be a number.

    >>> import numpy as np
    >>> x = Integer(10)
    >>> np.sqrt(x)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Integer' object has no attribute 'sqrt'
    

    The message itself comes from numpy, which apparently tries to call a sqrt method on unknown objects.

    I don't think we should work around this in matplotlib (or even just try to make a better error message). That's something to discuss between sympy and numpy.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions