On Fri, May 15, 2009 at 6:41 AM, Bala subramanian
<bala.biophys...@gmail.com> wrote:
> Friends,
> I have a data like the following.  I dnt want to plot the data literally as
> shown below. I want to define a color red for 0, blue for 1 and yellow for
> 2. Each color represents the properly of the y-values. I want to define
> colors for values and monitor the color changes with time.  I checked for
> color definition in the documentation but i cudnt make out anything.  Is it
> possible.  I would greatly appreciate any examples for the same.

The example belows shows the guts of what needs to be done.  It
creates a dictionary mapping value to color, and uses a
BrokenBarCollection to plot the regions updating them with a list of
new facecolors on every step.  Note that the "pylab animation" I use
below is for illustrative purposes only and is not supported -- rather
you need to embed the update calls in a GUI idle, timer or other
handler, as in the examples at

  http://matplotlib.sourceforge.net/examples/animation/index.html

But since I don't know what GUI you are using, I'll leave this part as
an exercise :-)

import matplotlib.pyplot as plt
from matplotlib.colors import colorConverter
import matplotlib.collections as collections
import numpy as np

Ncol = 7
x = np.array("""\
  0      0      0    1      1      1     1     1
  1      0      0    1      1      1     1     1
  2      0      0    1      1      1     1     1
  3      0      0    1      1      1     1     1
  4      0      0    1      1      1     1     1
  5      0      0    1      1      1     1     1
  6      0      0    1      1      1     1     1
  7      0      0    1      1      1     1     1
  8      0      0    1      1      0     0     1
  9      0      0    2      2      0     0     1
  10     1      1    2      2      0     0     1
  11     1      1    2      2      0     0     1
  12     1      1    2      2      0     0     1
  13     1      1    2      2      0     0     1
  14     1      1    2      2      0     0     1
  15     1      1    2      2      0     0     1
""".split(), int)

x = x.reshape(len(x)/(Ncol+1), Ncol+1)



# map value to color -- the color converter returns rgba which is what
# the collection wants
colors = (0,'red'), (1,'blue'), (2,'yellow')
colord = dict([(code, colorConverter.to_rgba(color)) for code, color in colors])

Ncol = 7

# this is for "pylab animation"
plt.ion()

xranges = [(xmin, xmin+1) for xmin in range(Ncol)]

collection = collections.BrokenBarHCollection(xranges, [0, 1])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_collection(collection)
ax.set_xlim(0, Ncol+1)
ax.set_ylim(-0.1, 1.1)

for row in x:
    t = row[0]
    vals = row[1:]
    colors = [colord[val] for val in vals]
    collection.set_facecolors(colors)
    fig.canvas.draw()

plt.show()

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to