Skip to content

DataFrame.plot.scatter() raises a TypeError when plotting bubble plots #12466

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

Closed
frankzhewang opened this issue Feb 26, 2016 · 1 comment
Closed
Labels
Duplicate Report Duplicate issue or pull request Visualization plotting

Comments

@frankzhewang
Copy link

Issue

The following code will raise a TypeError exception:

from pandas import DataFrame
import numpy as np

df = DataFrame(np.random.randn(5,2), columns=['x', 'y'])
df['s'] = np.random.random_integers(low=100, high=200, size=5)
df.plot.scatter(x='x', y='y', s='s')

However, there's no problem in using matplotlib directly:

from pandas import DataFrame
import numpy as np
import matplotlib.pyplot as plt

df = DataFrame(np.random.randn(5,2), columns=['x', 'y'])
df['s'] = np.random.random_integers(low=100, high=200, size=5)
plt.scatter(x=df['x'], y=df['y'], s=df['s'])

Note: This issue only arises when argument s is used. The ordinary x-y scatter plot works fine.

Traceback details

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-a2c96d4293aa> in <module>()
      4 df = DataFrame(np.random.randn(5,2), columns=['x', 'y'])
      5 df['s'] = np.random.random_integers(low=100, high=200, size=5)
----> 6 df.plot.scatter(x='x', y='y', s='s')

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in scatter(self, x, y, s, c, **kwds)
   3847         axes : matplotlib.AxesSubplot or np.array of them
   3848         """
-> 3849         return self(kind='scatter', x=x, y=y, c=c, s=s, **kwds)
   3850 
   3851     def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None,

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   3669                           fontsize=fontsize, colormap=colormap, table=table,
   3670                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3671                           sort_columns=sort_columns, **kwds)
   3672     __call__.__doc__ = plot_frame.__doc__
   3673 

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2554                  yerr=yerr, xerr=xerr,
   2555                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 2556                  **kwds)
   2557 
   2558 

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   2382         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   2383 
-> 2384     plot_obj.generate()
   2385     plot_obj.draw()
   2386     return plot_obj.result

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in generate(self)
    985         self._compute_plot_data()
    986         self._setup_subplots()
--> 987         self._make_plot()
    988         self._add_table()
    989         self._make_legend()

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/pandas/tools/plotting.py in _make_plot(self)
   1557             label = None
   1558         scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
-> 1559                              label=label, cmap=cmap, **self.kwds)
   1560         if cb:
   1561             img = ax.collections[0]

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1810                     warnings.warn(msg % (label_namer, func.__name__),
   1811                                   RuntimeWarning, stacklevel=2)
-> 1812             return func(ax, *args, **kwargs)
   1813         pre_doc = inner.__doc__
   1814         if pre_doc is None:

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   3891                 offsets=offsets,
   3892                 transOffset=kwargs.pop('transform', self.transData),
-> 3893                 alpha=alpha
   3894                 )
   3895         collection.set_transform(mtransforms.IdentityTransform())

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/matplotlib/collections.py in __init__(self, paths, sizes, **kwargs)
    831         Collection.__init__(self, **kwargs)
    832         self.set_paths(paths)
--> 833         self.set_sizes(sizes)
    834         self.stale = True
    835 

/Users/fzw/anaconda/envs/ds/lib/python3.5/site-packages/matplotlib/collections.py in set_sizes(self, sizes, dpi)
    804             self._sizes = np.asarray(sizes)
    805             self._transforms = np.zeros((len(self._sizes), 3, 3))
--> 806             scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
    807             self._transforms[:, 0, 0] = scale
    808             self._transforms[:, 1, 1] = scale

TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Darwin
OS-release: 13.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.17.1
nose: None
pip: 8.0.1
setuptools: 19.4
Cython: None
numpy: 1.10.2
scipy: 0.16.1
statsmodels: None
IPython: 4.0.3
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
Jinja2: 2.8
@TomAugspurger
Copy link
Contributor

Dupe of #8244

Basically we've punted on it for now since picking the appropriate size is a bit difficult. You can pass the array in directly with plt.scatter(x=df['x'], y=df['y'], s=df['s'].values)

If you've got any thoughts, feel free to post in #8244.

@TomAugspurger TomAugspurger added Visualization plotting Duplicate Report Duplicate issue or pull request labels Feb 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Visualization plotting
Projects
None yet
Development

No branches or pull requests

2 participants