Defining Charts
Charts help visualize model results and compare values. GAML provides several kinds of charts, including histograms, pie charts, series, radar charts, heatmaps and box-and-whisker plots. Choose the kind and data that match the question you want to answer.
Define a chart​
To define a chart, use the chart statement inside a display. Give the chart a name and select its kind with the kind facet. Available kinds include histogram, pie, series, scatter, xy, radar, heatmap and box_whisker. The older type facet of the chart statement is deprecated.
experiment my_experiment mode: gui {
output {
display my_display title: "my_display" {
chart "my_chart" kind:pie {
}
}
}
}
Configure a chart with facets such as x_serie_labels and y_serie_labels for axis labels, axes for axis colour, and additional facets for ranges, layout and style.
After declaring your chart, you have to define the data you want to display in your chart.
Data definition​
Data can be specified with:
- several
datastatements to specify each series. - one
dataliststatement to give a list of series. It can be useful if the number of series is unknown, variable or too high.
Use data to define one series: give it a legend, set its value with the value facet, and optionally set facets such as color.
global {
int numberA <- 2 update: numberA + 3;
int numberB <- 30 update: numberB + 1;
}
experiment my_experiment mode: gui {
float minimum_cycle_duration <- 0.1;
output {
display my_display title: "my_display" {
chart "my_chart" kind: pie {
data "numberA" value: numberA color: #red;
data "numberB" value: numberB color: #blue;
}
}
}
}
The screenshot below is a separate two-slice pie chart; it illustrates the chart form, while the code above shows how to declare data and colors.

The datalist statement defines several series at once. Its legend, value and optional facets take lists, with corresponding entries describing each series. The pie chart above can therefore be written with datalist as follows. This excerpt belongs inside an experiment output block:
display my_display2 title: "my_display2" {
chart "my_chart2" kind: pie {
datalist legend: ["numberA", "numberB"] value: [numberA, numberB] color: [#red, #blue];
}
}
Use datalist when the number of series can change during a simulation. In this model, it plots each people agent's energy; the population starts with three agents and grows to five, so the chart adds series as agents are created.
global {
init {
create people number: 3;
}
reflex population_growth when: length(people) < 5 {
create people number: 1;
}
}
species people {
int energy <- rnd(100) min: 0;
rgb color <- rnd_color(255);
reflex aging {
energy <- energy - 3;
}
}
experiment my_experiment mode: gui {
float minimum_cycle_duration <- 0.1;
output {
display my_display title: "my_display" {
chart "my_chart" kind: series {
datalist legend: people collect (person: person.name) value: people collect (person: person.energy) color: people collect (person: person.color);
}
}
}
}

datalist provides you some additional facets you can use. If you want to learn more about them, please read the documentation.
Various types of charts​
As we already said, you can display several types of graphs: the histograms, the pies, the series, the radars, heatmap...
pie​
The pie chart shows on a single pie diagram the ratio of each data series over the sum of all the series.
It has already been illustrated above.
series​
The series chart plots values over simulation steps, with time on the x-axis. The earlier pie-chart example can be displayed as a series by changing its kind to series.
global {
int numberA <- 2 update: numberA + 3;
int numberB <- 30 update: numberB + 1;
}
experiment my_experiment mode: gui {
float minimum_cycle_duration <- 0.1;
output {
display my_display title: "my_display" {
chart "my_chart" kind: series {
data "numberA" value: numberA color: #red;
data "numberB" value: numberB color: #blue;
}
}
}
}
histogram​
A histogram chart displays values as bars. The previous example can be displayed with a histogram chart.
Histograms can summarize a list of numeric observations. This complete model groups a small, fixed sample of ages into 10 bins between 0 and 100. distribution_of returns the labels and counts for those bins; series_label_position: xaxis displays the bin labels under the bars.
model AgeHistogramExample
global {
list<float> ages <- [5, 15, 25, 35, 45, 55, 65, 75, 85, 95,
15, 25, 35, 45, 55, 65, 75, 85,
25, 35, 45, 55, 65, 75,
35, 45, 55, 65,
45, 55];
}
experiment AgeHistogramExample mode: gui {
output {
display "Age distribution" {
chart "Age distribution" kind: histogram
title_font: font("SansSerif", 26, #bold)
tick_font: font("SansSerif", 18)
label_font: font("SansSerif", 20)
x_label: "Age group" y_label: "Number of observations"
x_tick_values_visible: false
series_label_position: xaxis {
datalist legend: (distribution_of(ages, 10, 0, 100) at "legend")
value: (distribution_of(ages, 10, 0, 100) at "values")
color: rgb(70, 120, 170);
}
}
}
}

The reverse_axis: true facet reverses the X and Y axes, for example to display horizontal bars.
xy​
An xy chart plots one value against another; its x-axis is not implicitly the simulation time. It can show a phase portrait, such as prey count against predator count in a Lotka–Volterra model. This excerpt belongs inside an experiment output block:
display PhasePortrait {
chart "Lotka Volterra Phase Portrait" kind: xy {
data 'Preys/Predators' value: {first(LotkaVolterra_agent).nb_prey, first(LotkaVolterra_agent).nb_predator} color: #black ;
}
}

radar​
A radar chart displays values around a circular axis. This example accumulates two values at each simulation step. The numbered axes show the recorded steps, and the red and blue polygons compare the two series:
model RadarExample
global {
int numberA <- 2 update: numberA + 1;
int numberB <- 4 update: numberB + 2;
}
experiment RadarExample mode: gui {
output {
display "Radar example" {
chart "Accumulated values" kind: radar
style: line background: #white axes: #black
title_font: font("SansSerif", 26, #bold)
tick_font: font("SansSerif", 20)
legend_font: font("SansSerif", 18)
y_range: {0, 12} {
data "numberA" value: numberA color: #red accumulate_values: true;
data "numberB" value: numberB color: #blue accumulate_values: true;
}
}
}
}
}

heatmap​
In a heatmap, the colour of each cell represents a value. With a distribution across the x-axis, successive simulation steps form rows that show how the distribution changes over time.
This model starts a population of 1,000 people. At each step, their age increases by one. Older agents have a higher probability of dying; each replacement starts at age zero.
model AgeDistribution
global {
init {
create people number: 1000;
}
}
species people {
float age <- truncated_gauss(50.0, 50.0);
reflex older {
age <- age + 1;
}
reflex die when: age >= 100 or flip(age / 1000) {
create people { age <- 0.0; }
do die();
}
}
experiment AgeDistribution mode: gui {
float minimum_cycle_duration <- 0.2;
output {
display "Age distribution by simulation step" {
chart "Age distribution by simulation step" kind: heatmap
x_serie_labels: (distribution_of(people collect each.age, 20, 0, 100) at "legend")
x_label: "Age range" y_label: "Simulation step" {
data "Age distribution"
value: (distribution_of(people collect each.age, 20, 0, 100) at "values")
color: #red;
}
}
}
}
Each row records one simulation step. The x-axis groups agents by age range, and the colour indicates the number of people in each range.

scatter​
The scatter chart plots x and y values for each sample; unlike a series chart, its x-axis does not represent simulation time implicitly. This complete model plots age against weight for 100 agents.
model AgeAndWeight
global {
init {
create people number: 100;
}
}
species people {
float age <- truncated_gauss(50.0, 50.0);
float weight <- age * 0.5 + rnd(0.0, 25.0);
}
experiment AgeAndWeight mode: gui {
output {
display "Age and weight" {
chart "Age and weight" kind: scatter x_label: "Age" y_label: "Weight" {
data "People" value: people collect (person: {person.age, person.weight})
accumulate_values: false line_visible: false;
}
}
}
}

box_whisker​
The box_whisker chart represents a distribution with its mean, median, first and third quartiles, and minimum and maximum. The box spans the first to third quartiles; the whiskers show the minimum and maximum.
For example, this model plots the population's age distribution at each simulation step. The value list supplies the mean, median, first and third quartiles, minimum and maximum, in that order. The boxes span the quartiles, the horizontal lines mark the medians, and the whiskers show the bounds.
model BoxWhiskerAge
global {
init {
create people number: 100;
}
}
species people {
float age <- rnd(0.0, 100.0);
reflex grow {
age <- age + 1;
}
reflex die when: age >= 100 {
create people { age <- 0.0; }
do die();
}
}
experiment BoxWhiskerAge mode: gui {
float minimum_cycle_duration <- 0.2;
output {
display "Age distribution over time" {
chart "Age distribution" kind: box_whisker
x_label: "Simulation step" y_label: "Age (years)" {
data "People"
value: [mean(people collect each.age),
median(people collect each.age),
quantile((people sort_by each.age) collect each.age, 0.25),
quantile((people sort_by each.age) collect each.age, 0.75),
min(people collect each.age),
max(people collect each.age)]
color: #green accumulate_values: true;
}
}
}
}

The series_label_position: yaxis facet places the series label (age) on the y-axis. Use series_label_position: legend to display series labels in a legend.
Other charts possibilities​
The chart, data and datalist statements provide additional facets for configuring chart layout and appearance. The examples below show a few of them.
Error values​
Error bars show a plotted value together with lower and upper bounds. For each cycle, this model plots the population's average age and uses its minimum and maximum ages as the error-bar endpoints. In y_err_values, each pair contains the lower endpoint followed by the upper endpoint.
model AgeErrorBars
global {
init {
create people number: 100;
}
}
species people {
float age <- rnd(35.0, 65.0);
reflex grow {
age <- age + 0.2;
}
reflex die when: age >= 70 {
create people { age <- 35.0; }
do die();
}
}
experiment AgeErrorBars mode: gui {
float minimum_cycle_duration <- 0.2;
output {
display "Average age and range" {
chart "Average age and population range" kind: series
x_label: "Simulation step" y_label: "Age (years)" y_range: {40, 60} {
data "Average age" value: people mean_of each.age
color: #red
y_err_values: [[people min_of each.age, people max_of each.age]];
}
}
}
}
