J Di Or Eximen
J Di Or Eximen

Reputation: 55

Use pylab.scatter and cmap in Python 3.5.1

When I use Python2, I can plot graphs

from sklearn import datasets
from matplotlib.colors import ListedColormap
circles = datasets.make_circles()

colors = ListedColormap(['red', 'blue'])
pyplot.figure(figsize(8, 8))

pyplot.scatter(map(lambda x: x[0], circles[0]), map(lambda x: x[1], circles[0]), c = circles[1], cmap = colors)

But when I use Python3, I can't do it. I tried to change color but couldn't.

I receive many errors:

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

During handling of the above exception, another exception occurred:

ValueError: to_rgba: Invalid rgba arg "[0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 0 1 0 1 1 0 1 0 1 0 1 1 0 0 0 0 1]" 
length of rgba sequence should be either 3 or 4

During handling of the above exception, another exception occurred:

ValueError: Color array must be two-dimensional

How do I fix this?

Upvotes: 3

Views: 750

Answers (1)

chthonicdaemon
chthonicdaemon

Reputation: 19770

The problem is that map does not return a list in Python 3. You can either pass the map to list or use a list comprehension, which is actually shorter than your lambda:

pyplot.scatter([x[0] for x in circles[0]], 
               [x[1] for x in circles[0]], c=circles[1], cmap=colors)

An even shorter version gets rid of the map completely:

 pyplot.scatter(*zip(*circles[0]), c=circles[1], cmap=colors)

Upvotes: 3

Related Questions