# Plotting bins using expressions

This example program lets the user specify some bins into which variable values will be mapped. Each bin gets an id 0, 1, 2, ... and the bins get colored using the Pseudocolor plot's color table. If colors are passed, then the user can specify the colors of the bins. VisIt expressions are used to map the variable into its proper bin and final bin numbers are assigned using the map expression.

• Note: with this approach, the default legend values will contain the computed bin ids instead of the ranges that were used to compute the bins. Some additional legend python coding might let the user override the bin labels though.
• VisIt 2.6 will offer a bin expression that simplifies the creation of the bin values.  Plotted variable colored by its bins. Plotted variable colored by its bins using custom colors.
```def UserDefinedFilledRegionPlot(var, bins, colors = None):
# Make bins using expressions.
DefineScalarExpression("bin0", "if(lte(%s, %g), 1, 0)" % (var, bins))

p2 = 2
for i in xrange(len(bins)-1):
DefineScalarExpression("bin%d" % (i+1), "if(and(gt(%s, %g), lte(%s, %g)), %s, 0)" % (var, bins[i], var, bins[i+1], p2))
p2 = p2 * 2

DefineScalarExpression("bin%d" % len(bins), "if(gt(%s, %g), %d, 0)" % (var, bins[-1], p2))

# Sum the bins and map them to 0,1,2,...
allbins = ""
p2list = []
p2 = 1
for i in xrange(len(bins)+1):
allbins = allbins + "+bin%d" % i
p2list = p2list + [p2]
p2 = p2 * 2
bvar = var + "_binned"
keys = str(p2list)
values = str(list(range(len(bins)+1)))
DefineScalarExpression(bvar, "map(%s, %s, %s)" % (allbins[1:], keys, values))

if colors != None:
ccpl = ColorControlPointList()
dx = 1. / float(len(colors)-1)
x = 0.
for i in xrange(len(colors)):
p = ColorControlPoint()
p.colors = (colors[i], colors[i], colors[i], 255)
p.position = x
x = x + dx
pc = PseudocolorAttributes(1)
pc.colorTableName = "custompc"
SetPlotOptions(pc)
DrawPlots()

# Plot "zrad", breaking things up into bins (,2], (2,5], (5,12], (12,15], (15,)
OpenDatabase("/usr/gapps/visit/data/noise.silo")

# Plot "zrad" again, this time passing some colors for the bins.
```bin0 = if(lte(zrad, 2), 1, 0) bin1 = if(and(gt(zrad, 2), lte(zrad, 5)), 2, 0) bin2 = if(and(gt(zrad, 5), lte(zrad, 12)), 4, 0) bin3 = if(and(gt(zrad, 12), lte(zrad, 15)), 8, 0) bin4 = if(gt(zrad, 15), 16, 0) zrad_binned = map(bin0+bin1+bin2+bin3+bin4, [1, 2, 4, 8, 16], [0, 1, 2, 3, 4]) ```
```zrad_binned = bin(zrad, [2,5,12,15])