Scatter plot should have a discrete colorbar when 'c' is integer · Issue #12380 · pandas-dev/pandas (original) (raw)
import pandas as pd
pd.set_option('max_rows', 10)
import sklearn.datasets as datasets
import pandas_ml as pdml # https://github.com/pandas-ml/pandas-ml
import matplotlib.pyplot as plt
df = pdml.ModelFrame(datasets.load_iris())
print(df)
.target sepal length (cm) sepal width (cm) petal length (cm)
0 0 5.1 3.5 1.4
1 0 4.9 3.0 1.4
2 0 4.7 3.2 1.3
3 0 4.6 3.1 1.5
4 0 5.0 3.6 1.4
.. ... ... ... ...
145 2 6.7 3.0 5.2
146 2 6.3 2.5 5.0
147 2 6.5 3.0 5.2
148 2 6.2 3.4 5.4
149 2 5.9 3.0 5.1
petal width (cm)
0 0.2 1 0.2 2 0.2 3 0.2 4 0.2 .. ... 145 2.3 146 1.9 147 2.0 148 2.3 149 1.8
[150 rows x 5 columns]
print(df.dtypes) .target int64 sepal length (cm) float64 sepal width (cm) float64 petal length (cm) float64 petal width (cm) float64 dtype: object df.plot.scatter(x='sepal length (cm)', y='sepal width (cm)', c='.target')
plt.show()
Converting .target
column to a category doesn't fix the problem.
Maybe I should open an other issue about it.
df['.target'] = df['.target'].astype('category') df.plot.scatter(x='sepal length (cm)', y='sepal width (cm)', c='.target')
ValueError Traceback (most recent call last) //anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgb(self, arg) 305 else: --> 306 fl = float(argl) 307 if fl < 0 or fl > 1:
ValueError: could not convert string to float: '.target'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last) //anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba(self, arg, alpha) 369 else: --> 370 r, g, b = self.to_rgb(arg) 371 if alpha is None:
//anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgb(self, arg) 327 raise ValueError( --> 328 'to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc)) 329 # Error messages could be improved by handling TypeError
ValueError: to_rgb: Invalid rgb arg ".target" could not convert string to float: '.target'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last) //anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba_array(self, c, alpha) 398 # Single value? Put it in an array with a single row. --> 399 return np.array([self.to_rgba(c, alpha)], dtype=np.float) 400 except ValueError:
//anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba(self, arg, alpha) 375 raise ValueError( --> 376 'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc)) 377
ValueError: to_rgba: Invalid rgba arg ".target" to_rgb: Invalid rgb arg ".target" could not convert string to float: '.target'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last) //anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgb(self, arg) 305 else: --> 306 fl = float(argl) 307 if fl < 0 or fl > 1:
ValueError: could not convert string to float: '.'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last) //anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba(self, arg, alpha) 369 else: --> 370 r, g, b = self.to_rgb(arg) 371 if alpha is None:
//anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgb(self, arg) 327 raise ValueError( --> 328 'to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc)) 329 # Error messages could be improved by handling TypeError
ValueError: to_rgb: Invalid rgb arg "." could not convert string to float: '.'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last) in () ----> 1 df.plot.scatter(x='sepal length (cm)', y='sepal width (cm)', c='.target')
//anaconda/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,
//anaconda/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
//anaconda/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
//anaconda/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
//anaconda/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()
//anaconda/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]
//anaconda/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:
//anaconda/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())
//anaconda/lib/python3.5/site-packages/matplotlib/collections.py in init(self, paths, sizes, **kwargs) 829 """ 830 --> 831 Collection.init(self, **kwargs) 832 self.set_paths(paths) 833 self.set_sizes(sizes)
//anaconda/lib/python3.5/site-packages/matplotlib/collections.py in init(self, edgecolors, facecolors, linewidths, linestyles, antialiaseds, offsets, transOffset, norm, cmap, pickradius, hatch, urls, offset_position, zorder, **kwargs) 115 116 self.set_edgecolor(edgecolors) --> 117 self.set_facecolor(facecolors) 118 self.set_linewidth(linewidths) 119 self.set_linestyle(linestyles)
//anaconda/lib/python3.5/site-packages/matplotlib/collections.py in set_facecolor(self, c) 610 c = mpl.rcParams['patch.facecolor'] 611 self._facecolors_original = c --> 612 self._facecolors = mcolors.colorConverter.to_rgba_array(c, self._alpha) 613 self.stale = True 614
//anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba_array(self, c, alpha) 420 result = np.zeros((nc, 4), dtype=np.float) 421 for i, cc in enumerate(c): --> 422 result[i] = self.to_rgba(cc, alpha) 423 return result 424
//anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba(self, arg, alpha) 374 except (TypeError, ValueError) as exc: 375 raise ValueError( --> 376 'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc)) 377 378 def to_rgba_array(self, c, alpha=None):
ValueError: to_rgba: Invalid rgba arg "." to_rgb: Invalid rgb arg "." could not convert string to float: '.'