# Plot a sequence of zeros or ones

I have a long list composed of either 0s or 1s.
I would like to plot the sequence as a horizontal bar in which 0 = black
pixel, 1 = white pixel
- something looking like a barcode, if you know what I mean.

Is there a way do to that with matplotlib?

Thanks

Something like the following should do the trick:

from pylab import figure, show, cm, nx
from matplotlib.colors import LinearSegmentedColormap

# make a binary, black and white colormap
cmapdata = {
'red' : ((0., 1., 1.), (1., 0., 0.)),
'green': ((0., 1., 1.), (1., 0., 0.)),
'blue' : ((0., 1., 1.), (1., 0., 0.))
}
binary = LinearSegmentedColormap('binary', cmapdata, 2)

fig = figure()
# a vertical barcode
x = nx.mlab.rand(500,1)
x[x>0.8] = 1.
x[x<=0.8] = 0.
ax = fig.add_axes([0.1, 0.3, 0.1, 0.6], xticks=[], yticks=[])
ax.imshow(x, aspect='auto', cmap=binary)

# a horizontal barcode
x = nx.mlab.rand(1,500)
x[x>0.8] = 1.
x[x<=0.8] = 0.
ax = fig.add_axes([0.3, 0.1, 0.6, 0.1], xticks=[], yticks=[])
ax.imshow(x, aspect='auto', cmap=binary)
show()

···

On 2/13/07, Giorgio Gilestro <giorgio@...1462...> wrote:

I have a long list composed of either 0s or 1s.
I would like to plot the sequence as a horizontal bar in which 0 = black
pixel, 1 = white pixel
- something looking like a barcode, if you know what I mean.

Is there a way do to that with matplotlib?

A minor modification: for a barcode, you'll want to pass
interpolation='nearest' to the imshow command. I just committed the
binary colormap to svn and added examples/barcode_demo.py. The new
version below puts it all together (but requires svn):

from pylab import figure, show, cm, nx

axprops = dict(xticks=[], yticks=[])
barprops = dict(aspect='auto', cmap=cm.binary, interpolation='nearest')

fig = figure()

# a vertical barcode
x = nx.mlab.rand(500,1)
x[x>0.8] = 1.
x[x<=0.8] = 0.
ax = fig.add_axes([0.1, 0.3, 0.1, 0.6], **axprops)
ax.imshow(x, **barprops)

# a horizontal barcode
x = nx.mlab.rand(1,500)
x[x>0.8] = 1.
x[x<=0.8] = 0.
ax = fig.add_axes([0.3, 0.1, 0.6, 0.1], **axprops)
ax.imshow(x, **barprops)

fig.savefig('barcode.png', dpi=100)
show()

···

On 2/13/07, John Hunter <jdh2358@...287...> wrote:

Something like the following should do the trick: