Denys
Denys

Reputation: 4557

plotting list of tuples (x coord, y coord, color) with pyplot.scatter() method

Firstly I create a list of tuples (points that are to be plotted). Every tuple consists of 3 numbers (x - coord, y - coord, c - colour of the point)

import random
import matplotlib.pyplot as plt

low = 1
high = 20
count = 100

data_points = [(random.randint(low,high), random.randint(low,high),
                random.randint(low,high))for x in range(count)]

Now i want to plot it with the pyplot.scatter() method.

If i specify my plot like this:

plt.scatter(*zip(*data_points))
plt.show()

than it reads the first two lists as x and y coordinates (which is correct), but the third list is taken as a size of a point (which is correct regarding the documentation). How do i tell python to read my third list as a color attribute (which is the 4th argument of the method) of the pyplot.scatter() method??

P.S.

matplotlib.pyplot.scatter(x, y, s=20, c=u'b', marker=u'o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs)

Upvotes: 2

Views: 4889

Answers (1)

Julien Spronck
Julien Spronck

Reputation: 15433

If you can change data_points and are okay with the additional overhead, you can simply add an extra number to specify the size:

data_points = [(random.randint(low,high), random.randint(low,high),
                15, random.randint(low,high))for x in range(count)]
plt.scatter(*zip(*data_points))
plt.show()

Upvotes: 3

Related Questions