3

I am trying to plot data points according to their class labels.

import numpy
import matplotlib as plt
x = numpy.random.uniform(size = [1, 15])
labels = numpy.array([1,2,2,2,2,1,1,2,3,1,3,3,1,1, 3])
plt.plot(x, 'o', c = labels)

When I did the above, Python complained that the color values need to be 0, 1. Then I used

plt.plot(x, 'o', c = labels/max(labels))

There is no error generated. A plot window pops up, but there is nothing in the plot window. I am wondering what is the correct way to define the colors that are according to the data labels?

I am also trying to color nodes according to the class labels. This is done in networkx. A simple example is:

import networkx as nx
G=nx.complete_graph(5)
nx.draw(G, node_col = node_labels)

The array node_labels will be the labels of the 5 vertices. I tried using the same approaches I tried above, but the network always has red nodes.

Any insight will be appreciated. Thanks!

user2498497
  • 693
  • 2
  • 14
  • 22
  • 1
    http://stackoverflow.com/questions/19311498/matplotlib-plot-with-a-different-color-for-certain-data-points – Joran Beasley Jul 17 '14 at 18:37
  • 1
    The first problem is that calling `numpy.random.uniform(size = [1, 15])` gives you an array within an array. Open up a console and type it in. Calling `numpy.random.uniform(size = 15)` gives you just one array. I'm also having trouble following your code. I can't run this. I don't know what version of Python you're using, but you seem to be missing some stuff: I have to `import pylab` and call `pylab.plot(x)`, and then `pylab.show()` to actually show the figure. At the very least, just importing `Matplotlib` and then typing `plot(x)`, rather than `Matplotlib.plot(x)` is not going to work. – Newb Jul 17 '14 at 18:38

2 Answers2

3

Since your labels are integers you can use them as an index for a list of colors:

colors = ['#e41a1c', '#377eb8', '#4daf4a']

then, using scatter is simpler than plot since you can provide a list/sequence of colors:

labels = np.random.randint(low=0, high=3, size=20)
plt.scatter(np.random.rand(20), np.random.rand(20), color=np.array(colors)[labels])

Which will give you this:

enter image description here

To get nice colors you can use colorbrewer.

user2304916
  • 7,882
  • 5
  • 39
  • 53
  • Thanks! I tried to use your method to plot the network and specified colors for the nodes. However the network still only plots red nodes.... But thanks for the info on scatter plot! – user2498497 Jul 17 '14 at 19:25
  • Oh Actually your method works in both scatter plot and network plot! Thanks. – user2498497 Jul 17 '14 at 19:40
0

In order to do what you're seeking, your labels array must be a floating-point array. From the look of it, [labels] is being interpreted as a integer array. Thus, modify your code as follows to achieve the desired result.

plt.plot(x, 'o', c = labels)

should be changed to:

plt.plot(x, 'o', c = labels.astype(numpy.float) 

Stay awesome!!

Matt Ellis
  • 451
  • 3
  • 6