Displaying several points with different colors

Hi,

I’m trying to plot a set of points, each point having a different color.
For the moment, I’m trying to do something like that :

for indice in range(0, points.shape[0]):
pl.plot(points[indice, 0], points[indice, 1], ‘o’, c = colours[indice,:], hold = True)

where points is a numpy array of point and colours is a numpy array of dimension (points.shape[0], 3)

With Matlab, something like this would function, but not in matplotlib, it tells me :
ValueError: need more than 0 values to unpack

I am missing something ?

Matthieu

Complete examples always help ince we have no way of knowing what the
points data structures look like, but I'll hazard a gues. The x and y
arguments to "plot" need to be sequences. Ie, something like

  plot([0.5], [0.5], 'ro')

It can be inefficient to plot many separate points this way -- if you
have a lot of points, use a
matplotlib.collections.RegularPolyCollection or scatter.

JDH

···

On 3/9/07, Matthieu Brucher <matthieu.brucher@...287...> wrote:

Hi,

I'm trying to plot a set of points, each point having a different color.
For the moment, I'm trying to do something like that :

  for indice in range(0, points.shape[0]):
    pl.plot(points[indice, 0], points[indice, 1], 'o', c =
colours[indice,:], hold = True)

where points is a numpy array of point and colours is a numpy array of
dimension (points.shape[0], 3)

With Matlab, something like this would function, but not in matplotlib, it
tells me :
ValueError: need more than 0 values to unpack

Complete examples always help ince we have no way of knowing what the
points data structures look like, but I’ll hazard a gues. The x and y

arguments to “plot” need to be sequences. Ie, something like

plot([0.5], [0.5], ‘ro’)

It can be inefficient to plot many separate points this way – if you
have a lot of points, use a

matplotlib.collections.RegularPolyCollection or scatter.

Thank you for your answer :slight_smile:

OK, I’ll try to wrap them :|. The best solution would indeed be to paint them all at once, but I didn’t manage to do that, even when I looked in the scatter_demo2 example :frowning:

What I have is a set of points in a numpy.array - for instance size (2000, 2) -. What I have as well is a nump.array of size (2000, 3). How can I make plot understand that each line of the set points must be painted with the corresponding line in the colour array ?

Matthieu

What I have is a set of points in a numpy.array - for instance size (2000,
2) -. What I have as well is a nump.array of size (2000, 3). How can I make
plot understand that each line of the set points must be painted with the
corresponding line in the colour array ?

You'll probably want to use a regular poly collection, as in the
example below. Your array of facecolors can pretty much be whatever
you want:

from pylab import figure, nx, show, cm
from matplotlib.collections import RegularPolyCollection

fig = figure()
ax = fig.add_subplot(111)

offsets = nx.mlab.rand(20,2)
facecolors = [cm.jet(x) for x in nx.mlab.rand(20)]
black = (0,0,0,1)

collection = RegularPolyCollection(
    fig.dpi,
    numsides=5, # a pentagon
    rotation=0,
    sizes=(50,),
    facecolors = facecolors,
    edgecolors = (black,),
    linewidths = (1,),
    offsets = offsets,
    transOffset = ax.transData,
    )

ax.add_collection(collection)

show()

···

On 3/9/07, Matthieu Brucher <matthieu.brucher@...287...> wrote:

Matthieu

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thank you, I think thatthis will solve my problem :slight_smile:
I didn’t know this class existed.

Matthieu

2007/3/9, John Hunter <jdh2358@…287…

···

:
On 3/9/07, Matthieu Brucher < > matthieu.brucher@…287…> wrote:

What I have is a set of points in a numpy.array - for instance size (2000,
2) -. What I have as well is a nump.array of size (2000, 3). How can I make
plot understand that each line of the set points must be painted with the

corresponding line in the colour array ?

You’ll probably want to use a regular poly collection, as in the
example below. Your array of facecolors can pretty much be whatever
you want:

from pylab import figure, nx, show, cm

from matplotlib.collections import RegularPolyCollection

fig = figure()
ax = fig.add_subplot(111)

offsets = nx.mlab.rand(20,2)
facecolors = [cm.jet(x) for x in nx.mlab.rand(20)]
black = (0,0,0,1)

collection = RegularPolyCollection(
fig.dpi,
numsides=5, # a pentagon
rotation=0,
sizes=(50,),
facecolors = facecolors,
edgecolors = (black,),
linewidths = (1,),

offsets = offsets,
transOffset = ax.transData,
)

ax.add_collection(collection)

show()

Matthieu


Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net’s Techsay panel and you’ll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash

http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I manage to do what I wanted to do, it was not that easy - the colours were in an array I had to transform into an array of tuples, the autoscale did not function, I had to put in the fig.subplot the correct xlim and ylim -, but now it works like a charm.

Many thanks.

Matthieu

2007/3/9, John Hunter <jdh2358@…287…>:

···

On 3/9/07, Matthieu Brucher <matthieu.brucher@…287…> wrote:

What I have is a set of points in a numpy.array - for instance size (2000,
2) -. What I have as well is a nump.array of size (2000, 3). How can I make
plot understand that each line of the set points must be painted with the
corresponding line in the colour array ?

You’ll probably want to use a regular poly collection, as in the

example below. Your array of facecolors can pretty much be whatever
you want:

from pylab import figure, nx, show, cm
from matplotlib.collections import RegularPolyCollection

fig = figure()

ax = fig.add_subplot(111)

offsets = nx.mlab.rand(20,2)
facecolors = [cm.jet(x) for x in nx.mlab.rand(20)]
black = (0,0,0,1)

collection = RegularPolyCollection(
fig.dpi,
numsides=5, # a pentagon

rotation=0,
sizes=(50,),
facecolors = facecolors,
edgecolors = (black,),
linewidths = (1,),
offsets = offsets,
transOffset = ax.transData,
)

ax.add_collection
(collection)

show()

Matthieu


Take Surveys. Earn Cash. Influence the Future of IT

Join SourceForge.net’s Techsay panel and you’ll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash

http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

OK, I’m trying to the same but in 3D, and there, no documentaion.
I tried to launch axes3d.py to see exactly what I can do, but the example file does not work. For instance, Axes3D takes two parameters for method init and only one is provided. Did someone test it ?

I’m trying to use collection as well, but for once I prefer MatLab simplicity here, it’s way to complicated, some arguments must be zipped, other are passed not in the collection, but when the collection is added to the subplot, …

Is there a simple and clear way to do 3D plotting with collections ?

Matthieu

2007/3/12, Matthieu Brucher <matthieu.brucher@…287…

···

:
I manage to do what I wanted to do, it was not that easy - the colours were in an array I had to transform into an array of tuples, the autoscale did not function, I had to put in the fig.subplot the correct xlim and ylim -, but now it works like a charm.

Many thanks.

Matthieu

2007/3/9, John Hunter <
jdh2358@…287…>:

On 3/9/07, Matthieu Brucher <matthieu.brucher@…287…> wrote:

What I have is a set of points in a numpy.array - for instance size (2000,
2) -. What I have as well is a nump.array of size (2000, 3). How can I make
plot understand that each line of the set points must be painted with the
corresponding line in the colour array ?

You’ll probably want to use a regular poly collection, as in the

example below. Your array of facecolors can pretty much be whatever
you want:

from pylab import figure, nx, show, cm
from matplotlib.collections import RegularPolyCollection

fig = figure()

ax = fig.add_subplot(111)

offsets = nx.mlab.rand(20,2)
facecolors = [cm.jet(x) for x in nx.mlab.rand(20)]
black = (0,0,0,1)

collection = RegularPolyCollection(
fig.dpi,
numsides=5, # a pentagon

rotation=0,
sizes=(50,),
facecolors = facecolors,
edgecolors = (black,),
linewidths = (1,),
offsets = offsets,
transOffset = ax.transData,
)

ax.add_collection
(collection)

show()

Matthieu


Take Surveys. Earn Cash. Influence the Future of IT

Join SourceForge.net’s Techsay panel and you’ll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
[

http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Matthieu Brucher wrote:

OK, I'm trying to the same but in 3D, and there, no documentaion.
I tried to launch axes3d.py to see exactly what I can do, but the example file does not work. For instance, Axes3D takes two parameters for method __init__ and only one is provided. Did someone test it ?
I'm trying to use collection as well, but for once I prefer MatLab simplicity here, it's way to complicated, some arguments must be zipped, other are passed not in the collection, but when the collection is added to the subplot, ...
Is there a simple and clear way to do 3D plotting with collections ?

There may be, but all 3D plotting is shaky right now. The 3D code is essentially unmaintained and unsupported.

Eric

There may be, but all 3D plotting is shaky right now. The 3D code is
essentially unmaintained and unsupported.

Eric

Sad to hear this, I hope someone will enhance it :slight_smile:

Matthieu

Matthieu Brucher wrote:

I manage to do what I wanted to do, it was not that easy - the colours were in an array I had to transform into an array of tuples, the autoscale did not function, I had to put in the fig.subplot the correct xlim and ylim -, but now it works like a charm.
Many thanks.

What version of mpl are you using?
In recent versions, the collections should accept 2D numpy arrays as well as any sequence of tuples (and several other possibilities).

Autoscaling does not work well with arbitrary collections because there is a chicken-and-egg problem: a collection member may be drawn in pixels or other physical units, in which case its size in data units cannot be determined until it is drawn with a given xlim and ylim (and sometimes dpi).

Eric

What version of mpl are you using?

The latest, I compiled it from the source as FC5 has a very old version - can’t update myself the distribution -

In recent versions, the collections should accept 2D numpy arrays as
well as any sequence of tuples (and several other possibilities).

For 2D plots, numpy arrays is accepted - but not for colors, it tells me there is a problem with tuples, I do not remember exactly, but I can check the error tomorrow -.
For 3D plots, the offsets must be special, I suppose, as the array must be iterated on with a zip, I don’t know why there is this need, but I didn’t write it :slight_smile: This difference in format is akward :expressionless: And the fact that the example file cannot be executed is still stranger.

Autoscaling does not work well with arbitrary collections because there
is a chicken-and-egg problem: a collection member may be drawn in pixels

or other physical units, in which case its size in data units cannot be
determined until it is drawn with a given xlim and ylim (and sometimes dpi).

That is not my worst problem, I imposed the limits and it works now like a charm :slight_smile:

Matthieu

Hi,

I tried to dig into the 3D code, and in fact, it is RegularPolyCollection that seems to be incompatible with Axes3D. Axes3D uses _verts to create z values, and it crashes when trying to figure out how to display it (
art3d.py lines 208-221). I don’t know enough to correct it.
But I have another idea, but I suppose some had it before. In the plot, scatter, … code, only one colour can be used, it is tested upon, so my question will be, why not enhancing the test to allow colors for each point ? it would be a sequence of the same size as x or y, but with rgba value, or tuples, …

Matthieu

P.S. : if I had more time, I would try to solve the problem, but at the moment, I can’t :frowning:

2007/3/12, Eric Firing <efiring@…202…

···

:
Matthieu Brucher wrote:

OK, I’m trying to the same but in 3D, and there, no documentaion.

I tried to launch axes3d.py to see exactly what I can do, but the
example file does not work. For instance, Axes3D takes two parameters
for method init and only one is provided. Did someone test it ?

I’m trying to use collection as well, but for once I prefer MatLab
simplicity here, it’s way to complicated, some arguments must be zipped,
other are passed not in the collection, but when the collection is added

to the subplot, …
Is there a simple and clear way to do 3D plotting with collections ?

There may be, but all 3D plotting is shaky right now. The 3D code is
essentially unmaintained and unsupported.

Eric