0

I want to plot up a data set of dimension 50*50. Each datum is associated with a "probability", a number between 0 and 1. I want to represent their probability by color gradation. I know how to plot up the dots using matplotlib tool. But how should I associate them with the appropriate colors?

Thanks!

Rick
  • 1
  • 1
  • 1
  • 1
  • 1
    Can you show us what you have tried? Have you read the [docs](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)? What about them didn't you understand or is not working as you expect? – tacaswell Jul 25 '13 at 20:45
  • 1
    to confirm you want to plot 2500 dimensions in a 2d space? ... – Eiyrioü von Kauyf Jul 25 '13 at 20:58
  • repeat? http://stackoverflow.com/questions/8202605/matplotlib-scatterplot-colour-as-a-function-of-a-third-variable – Brad Jul 25 '13 at 21:01
  • and http://stackoverflow.com/questions/17865240/plotting-clustered-data-in-matplotlib which was just asked. – tacaswell Jul 25 '13 at 21:46

2 Answers2

8

You need to use a colormap to associate a certain color to each point. For example consider the following piece of code

 import numpy as np
 from mpl_toolkits.mplot3d import Axes3D
 import matplotlib.pyplot as plt
 import random as ran

 fig = plt.figure()
 ax = fig.add_subplot(111, projection='3d')

 cmhot = plt.cm.get_cmap("hot")
 xs =  [ran.random()*50 for n in range(0,50)]
 ys = [ran.random()*50 for n in range(0,50)]
 zs = [ran.random() for n in range(0,50)]
 l = ax.scatter(xs, ys, zs, c=zs, cmap=cmhot)
 fig.colorbar(l)
 plt.show()

That results in this:

Scatter 3d plot

In this case the color is associated to the z value, by using the default "hot" colormap.

papafe
  • 2,959
  • 4
  • 41
  • 72
6

Something like this? The key point is c=z which tells scatter to apply the colormap specified with cmap to each point according to the scalar value of z.

import numpy
import matplotlib.pyplot as plt
x=numpy.random.randint(0,100,size=25)
y=numpy.random.randint(0,100,size=25)
z=numpy.random.rand(25)
plt.scatter(x, y, c=z, s=100, cmap=plt.cm.cool, edgecolors='None', alpha=0.75)
plt.colorbar()
plt.show()

You can also give a list of colors as the c argument. For example

colors=[(1,0,1,el) for el in z]
plt.scatter(x, y, c=colors, s=100, edgecolors='None')

In this case each point has a RGB tuple associated with it and scatter uses the color you specify. With the two lines given above you'd have the alpha value of each point vary according to the probability z. This you can not achieve with just the alpha keyword of scatter:

numentar
  • 1,049
  • 8
  • 21