12

I have the following data (four equal-length arrays) :

a = [1, 4, 5, 2, 8, 9, 4, 6, 1, 0, 6]
b = [4, 7, 8, 3, 0, 9, 6, 2, 3, 6, 7]
c = [9, 0, 7, 6, 5, 6, 3, 4, 1, 2, 2]
d = [La, Lb, Av, Ac, Av, By, Lh, By, Lg, Ac, Bt]

I am making a 3d plot of arrays a, b, c :

import pylab
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(a,b,c)

plt.show()

Now, I want to color these scattered points using the array named 'd' such that; if the first letter of corresponding 'i'th element value in d is 'L', then colour the point red, if it starts with 'A' colour it green and if it starts with 'B', colour it blue.

So, first point (1,4,9) should be red, second(4,7,0) red too, third(5,8,7) should be green and so on..

Is it possible to do so? Please help if you have some idea :)

3
  • Is d a list of strings or are those variable names? If strings, it should look like: d = ['La', 'Lb', 'Av', ...] Commented Oct 2, 2013 at 15:09
  • 1
    And presumably you are using from mpl_toolkits.mplot3d import Axes3D somewhere in your file. Commented Oct 2, 2013 at 15:31
  • You are right, it actually is d = ['La', 'Lb', 'Av', ...]. I have a complicated big problem i am working on and this is one small part where i got stuck.. hence I created this simple sample example to ask my question.. forgot to keep in mind these two small details.. sorry! Commented Oct 3, 2013 at 9:09

2 Answers 2

18

As the documentation for scatter explains, you can pass the c argument:

c : color or sequence of color, optional, default

c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.

So something like

use_colours = {"L": "red", "A": "green", "B": "blue"}
ax.scatter(a,b,c,c=[use_colours[x[0]] for x in d],s=50)

should produce

coloured points

Sign up to request clarification or add additional context in comments.

2 Comments

I was going to suggest using np.select instead of a dict, but you have to be clever to get the first char from an array of strings: c=np.select(map(lambda s: s==da.view('S1,S1')['f0'], ['L', 'A', 'B']),['red','green','blue']) or c=np.select(map(lambda s: s==da.view('S1,S1')['f0'], use_colours.keys()),use_colours.values()), but of course this isn't worth it unless the number of colours is small and the number of points is large.
@DSM : thank you, I did not expect it (assigning colour according to the first alphabet of corresponding words) to be so simple :) thanks a tonnes!!!
1

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter

c : color or sequence of color, optional, default

"c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however."

Have you tried this?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.