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!

4

2 Answers 2

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.

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:

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.