Skip to content
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

scatter color vs c #7603

Closed
smartinsightsfromdata opened this issue Dec 10, 2016 · 10 comments
Closed

scatter color vs c #7603

smartinsightsfromdata opened this issue Dec 10, 2016 · 10 comments

Comments

@smartinsightsfromdata
Copy link

smartinsightsfromdata commented Dec 10, 2016

The following code gives error, but works replacing color= withc=
I suspect is either a bug, or some piece of documentation missing?

I'm using:
matplotlib 1.5.3
pandas 0.19.1
python 3.5

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
%matplotlib inline

iris = load_iris()
colors = list()
palette = {0: "red", 1: "green", 2: "blue"}

for c in np.nditer(iris.target): colors.append(palette[int(c)])
    # using the palette dictionary, we convert
    # each numeric class into a color string
dataframe = pd.DataFrame(iris.data,
columns=iris.feature_names)
scatterplot = pd.scatter_matrix(dataframe, alpha=0.3,
figsize=(10, 10), diagonal='hist', color=colors, marker='o', grid=True)
@tacaswell
Copy link
Member

Can you reproduce this with out using pandas? Otherwise this probably should be reported against pandas.

@tacaswell
Copy link
Member

Can you also include the full traceback?

@smartinsightsfromdata
Copy link
Author

smartinsightsfromdata commented Dec 10, 2016

This is the full trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-f3d30057e124> in <module>()
     14 columns=iris.feature_names)
     15 scatterplot = pd.scatter_matrix(dataframe, alpha=0.3,
---> 16 figsize=(10, 10), diagonal='hist', color=colors, marker='o', grid=True)

/Users/e/anaconda/lib/python3.5/site-packages/pandas/tools/plotting.py in scatter_matrix(frame, alpha, figsize, ax, grid, diagonal, marker, density_kwds, hist_kwds, range_padding, **kwds)
    393 
    394                 ax.scatter(df[b][common], df[a][common],
--> 395                            marker=marker, alpha=alpha, **kwds)
    396 
    397                 ax.set_xlim(boundaries_list[j])

/Users/e/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1817                     warnings.warn(msg % (label_namer, func.__name__),
   1818                                   RuntimeWarning, stacklevel=2)
-> 1819             return func(ax, *args, **kwargs)
   1820         pre_doc = inner.__doc__
   1821         if pre_doc is None:

/Users/e/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)
   3787                 facecolors = co
   3788             if c is not None:
-> 3789                 raise ValueError("Supply a 'c' kwarg or a 'color' kwarg"
   3790                                  " but not both; they differ but"
   3791                                  " their functionalities overlap.")

ValueError: Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap.

@smartinsightsfromdata
Copy link
Author

smartinsightsfromdata commented Dec 10, 2016

I tried with just matpltlib. As before c works and color doesn't, but I get a different error.

Incidentally, I couldn't find anywhere the reference on the difference between color kwargs and c kwargs mentioned in the trace.

import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses
plt.scatter(x, y, s=area, color=colors, alpha=0.5)

This is the trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/e/anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba(self, arg, alpha)
    367                     raise ValueError(
--> 368                             'length of rgba sequence should be either 3 or 4')
    369             else:

ValueError: length of rgba sequence should be either 3 or 4

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/Users/e/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:

/Users/e/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 "[ 0.51212644  0.22378055  0.06807402  0.43638562  0.68215248  0.1378158
  0.15249078  0.55415346  0.47947919  0.12748865  0.72954419  0.37898197
  0.42483739  0.1797149   0.59384571  0.54982906  0.05255149  0.4734899
  0.90948923  0.08066408  0.22102738  0.77243848  0.05687152  0.38126391
  0.05717766  0.3300619   0.81012634  0.60052149  0.46439006  0.86098937
  0.93625576  0.16322075  0.14090725  0.88908521  0.32210086  0.55698194
  0.18936879  0.7390388   0.03459946  0.85838907  0.22985901  0.15620236
  0.58757227  0.96806694  0.58318205  0.00226799  0.00164644  0.2462151
  0.9577508   0.57849709]"
length of rgba sequence should be either 3 or 4

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/Users/e/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)
   3777             try:
-> 3778                 mcolors.colorConverter.to_rgba_array(co)
   3779             except ValueError:

/Users/e/anaconda/lib/python3.5/site-packages/matplotlib/colors.py in to_rgba_array(self, c, alpha)
    402                 if c.ndim != 2 and c.dtype.kind not in 'SU':
--> 403                     raise ValueError("Color array must be two-dimensional")
    404                 if (c.ndim == 2 and c.shape[1] == 4 and c.dtype.kind == 'f'):

ValueError: Color array must be two-dimensional

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-9-68c680282e55> in <module>()
      9 area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses
     10 
---> 11 plt.scatter(x, y, s=area, color=colors, alpha=0.5)

/Users/e/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3256                          vmin=vmin, vmax=vmax, alpha=alpha,
   3257                          linewidths=linewidths, verts=verts,
-> 3258                          edgecolors=edgecolors, data=data, **kwargs)
   3259     finally:
   3260         ax.hold(washold)

/Users/e/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1817                     warnings.warn(msg % (label_namer, func.__name__),
   1818                                   RuntimeWarning, stacklevel=2)
-> 1819             return func(ax, *args, **kwargs)
   1820         pre_doc = inner.__doc__
   1821         if pre_doc is None:

/Users/e/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)
   3778                 mcolors.colorConverter.to_rgba_array(co)
   3779             except ValueError:
-> 3780                 raise ValueError("'color' kwarg must be an mpl color"
   3781                                  " spec or sequence of color specs.\n"
   3782                                  "For a sequence of values to be"

ValueError: 'color' kwarg must be an mpl color spec or sequence of color specs.
For a sequence of values to be color-mapped, use the 'c' kwarg instead.

@efiring
Copy link
Member

efiring commented Dec 10, 2016

ValueError: 'color' kwarg must be an mpl color spec or sequence of color specs.
For a sequence of values to be color-mapped, use the 'c' kwarg instead.

That last exception message is telling you what the problem is, and how to fix it, so matplotlib is behaving as intended. I don't know what the problem is on the pandas side.

@goyodiaz
Copy link

goyodiaz commented Dec 10, 2016

It looks like a pandas issue. pandas.scatter_matrix always pass c to scatter but the caller can optionally pass more keywords arguments, including color. If the caller pass only color then scatter_matrix will pass both c and color and matplotlib will raise with a message that is confusing to the user because they did not pass c. Probably scatter_matrix should either raise with a clearer message or drop the c keyword argument when the user pass color.

There is still a potential source of confusion in the matplotlib side. I guess many users will use color instead of c because in other functions like plot they are equivalent. Maybe a comment should be added to the scatter docstring stating that c and color are actually different and incompatible.

@smartinsightsfromdata
Copy link
Author

@goyodiaz Thanks of the comment.
Before this error I didn't have it clear that c and color where different and some clarification in the docs would help.

I will also raise this error on pandas.

@Kojoley
Copy link
Member

Kojoley commented Dec 28, 2016

Closed as not a matplotlib issue.

@Kojoley Kojoley closed this as completed Dec 28, 2016
@corner100
Copy link

This is working:

x = rng.randn(12)
y = rng.randn(12)
colors = rng.rand(12)
sizes = 1000 * rng.rand(12)

plt.scatter(x, y, c=colors, s=sizes, alpha=0.3,
            cmap='viridis')
plt.colorbar();  # show color scale
plt.show()

This is not working:

x = rng.randn(12)
y = rng.randn(12)
colors = rng.rand(12)
sizes = 1000 * rng.rand(12)

plt.scatter(x, y, colors=colors, s=sizes, alpha=0.3,
            cmap='viridis')
plt.colorbar();  # show color scale
plt.show()

@jklymak
Copy link
Member

jklymak commented Dec 4, 2018

Did you try color Instead of colors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants