我有以下pandas数据框名为df:
quantity receita ticket_medio
PERFUMARIA 0.5 50 0.398174
SABONETE 0.2 11 0.128486
CORPO 0.1 17 0.116748
使用上面的数据框,我想生成一个气泡图,其中x轴是receita,y轴是数量,ticket_medio是气泡大小。
我尝试了以下代码:
fig, ax = plt.subplots(figsize=(15, 10))
df.plot.scatter('receita', 'quantity', ax=ax, s=df['ticket_medio'])
for k, v in df[['receita', 'quantity']].iterrows():
ax.annotate(k, v)
plt.show()
但是我收到以下错误消息:
AttributeError Traceback (most recent call last)
<ipython-input-117-93c733a91344> in <module>()
1 fig, ax = plt.subplots(figsize=(15, 10))
----> 2 category_frequency.plot.scatter('receita', 'quantity', ax=ax, s=category_frequency['ticket_medio'])
3 for k, v in category_frequency[['receita', 'quantity']].iterrows():
4 ax.annotate(k, v)
5 plt.show()
~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in scatter(self, x, y, s, c, **kwds)
2793 axes : matplotlib.AxesSubplot or np.array of them
2794 """
-> 2795 return self(kind='scatter', x=x, y=y, c=c, s=s, **kwds)
2796
2797 def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None,
~\Anaconda3\lib\site-packages\pandas\plotting\_core.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)
2615 fontsize=fontsize, colormap=colormap, table=table,
2616 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2617 sort_columns=sort_columns, **kwds)
2618 __call__.__doc__ = plot_frame.__doc__
2619
~\Anaconda3\lib\site-packages\pandas\plotting\_core.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)
1857 yerr=yerr, xerr=xerr,
1858 secondary_y=secondary_y, sort_columns=sort_columns,
-> 1859 **kwds)
1860
1861
~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
1682 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
1683
-> 1684 plot_obj.generate()
1685 plot_obj.draw()
1686 return plot_obj.result
~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in generate(self)
238 self._compute_plot_data()
239 self._setup_subplots()
--> 240 self._make_plot()
241 self._add_table()
242 self._make_legend()
~\Anaconda3\lib\site-packages\pandas\plotting\_core.py in _make_plot(self)
832 label = None
833 scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
--> 834 label=label, cmap=cmap, **self.kwds)
835 if cb:
836 img = ax.collections[0]
~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1895 warnings.warn(msg % (label_namer, func.__name__),
1896 RuntimeWarning, stacklevel=2)
-> 1897 return func(ax, *args, **kwargs)
1898 pre_doc = inner.__doc__
1899 if pre_doc is None:
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
4032 offsets=offsets,
4033 transOffset=kwargs.pop('transform', self.transData),
-> 4034 alpha=alpha
4035 )
4036 collection.set_transform(mtransforms.IdentityTransform())
~\Anaconda3\lib\site-packages\matplotlib\collections.py in __init__(self, paths, sizes, **kwargs)
902 Collection.__init__(self, **kwargs)
903 self.set_paths(paths)
--> 904 self.set_sizes(sizes)
905 self.stale = True
906
~\Anaconda3\lib\site-packages\matplotlib\collections.py in set_sizes(self, sizes, dpi)
875 self._sizes = np.asarray(sizes)
876 self._transforms = np.zeros((len(self._sizes), 3, 3))
--> 877 scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
878 self._transforms[:, 0, 0] = scale
879 self._transforms[:, 1, 1] = scale
AttributeError: 'numpy.float64' object has no attribute 'sqrt'
我该如何解决?
非常感谢,
罗德里戈。