Histogram Output Example
Location: examples/rappture2/histogram/
This example demonstrates the <histogram> output type for bar charts,
including single and grouped histograms.
Script
import sys
import numpy as np
import rappture2web.rp_library as Rappture
rx = Rappture.PyXml(sys.argv[1])
num_points = int(rx['input.(points).current'].value)
x = np.linspace(1, 10, num_points)
# Single histogram
hist = rx['output.histogram(single)']
hist['about.label'] = 'Single histogram'
hist['xaxis.label'] = 'Time'
hist['xaxis.units'] = 's'
hist['yaxis.label'] = 'Voltage v(11)'
hist['yaxis.units'] = 'V'
hist['component.xy'] = (x, np.cos(x) / (1 + x))
# Grouped histograms (overlaid on same axes)
for factor in [1, 2]:
hist = rx['output.histogram(multi%s)' % factor]
hist['about.group'] = 'Multiple histogram'
hist['about.label'] = 'Factor a=%s' % factor
hist['xaxis.label'] = 'Frequency'
hist['yaxis.label'] = 'Current'
hist['component.xy'] = (x, np.power(2.0, factor * x) / x)
rx.close()
Key concepts
Set
about.groupto the same name on multiple histograms to overlay them on a single plot.Axis labels and units are configured the same way as curves.
tool.xml
<?xml version="1.0"?>
<run xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../rappture2web/contract.xsd">
<tool>
<title>histogram (rappture2web)</title>
<about>Example of a Rappture <histogram> object.
Histograms are a list of (x,y) data points. When you click Simulate, the inputs generate output histograms.
</about>
<command>python3 @tool/histogram.py @driver</command>
<contract>
<input>
<integer id="points">
<about>
<label>Number of points</label>
<description>Determines the number of data points in all output histograms.</description>
</about>
<min>2</min>
<max>1000</max>
<default>10</default>
</integer>
</input>
<output>
<histogram id="single"><about><label>Single histogram</label><description>Example of a single histogram.</description></about><xaxis><label>Time</label><units>s</units></xaxis><yaxis><label>Voltage v(11)</label><units>V</units></yaxis></histogram>
<histogram id="multi1"><about><label>Factor a=1</label><description>Grouped histogram for factor 1.</description></about><xaxis><label>Frequency</label><units>Hz</units></xaxis><yaxis><label>Current</label><units>uA</units></yaxis></histogram>
<histogram id="multi2"><about><label>Factor a=2</label><description>Grouped histogram for factor 2.</description></about><xaxis><label>Frequency</label><units>Hz</units></xaxis><yaxis><label>Current</label><units>uA</units></yaxis></histogram>
</output>
</contract>
</tool>
<input>
<integer id="points">
<about>
<label>Number of points</label>
<description>Determines the number of data points in all output histograms.</description>
</about>
<min>2</min>
<max>1000</max>
<default>10</default>
</integer>
</input>
</run>
Running
rappture2web examples/rappture2/histogram/