mesa.visualization.modules package

Submodules

mesa.visualization.modules.BarChartVisualization module

Pie Chart Module

Module for drawing live-updating bar charts using d3.js

class BarChartModule(fields, scope='model', sorting='none', sort_by='none', canvas_height=400, canvas_width=800, data_collector_name='datacollector')[source]

Bases: VisualizationElement

Each bar chart can either visualize model-level or agent-level fields from a datcollector

with a bar chart.

Attributes:

scope: whether to visualize agent-level or model-level fields fields: A List of Dictionaries containing information about each field to be charted,

including the name of the datacollector field and the desired color of the corresponding bar. Ex: [{“Label”:”<your field name>”, “Color”:”<your desired color in hex>”}]

sorting: Whether to sort ascending, descending, or neither when charting agent fields sort_by: The agent field to sort by canvas_height, canvas_width: The width and height to draw the chart on the page, in pixels.

Default to 800 x 400

data_collector_name: Name of the DataCollector object in the model to retrieve data from.

Create a new bar chart visualization.

Args:
scope: “model” if visualizing model-level fields, “agent” if visualizing agent-level

fields.

fields: A List of Dictionaries containing information about each field to be charted,

including the name of the datacollector field and the desired color of the corresponding bar. Ex: [{“Label”:”<your field name>”, “Color”:”<your desired color in hex>”}]

sorting: “ascending”, “descending”, or “none” sort_by: The agent field to sort by canvas_height, canvas_width: Size in pixels of the chart to draw. data_collector_name: Name of the DataCollector to use.

package_includes = ['external/d3-7.4.3.min.js', 'BarChartModule.js']
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.CanvasGridVisualization module

Modular Canvas Rendering

Module for visualizing model objects in grid cells.

class CanvasGrid(portrayal_method, grid_width, grid_height, canvas_width=500, canvas_height=500)[source]

Bases: VisualizationElement

A CanvasGrid object uses a user-provided portrayal method to generate a portrayal for each object. A portrayal is a JSON-ready dictionary which tells the relevant JavaScript code (GridDraw.js) where to draw what shape.

The render method returns a dictionary, keyed on layers, with values as lists of portrayals to draw. Portrayals themselves are generated by the user-provided portrayal_method, which accepts an object as an input and produces a portrayal of it.

A portrayal as a dictionary with the following structure:

“x”, “y”: Coordinates for the cell in which the object is placed. “Shape”: Can be either “circle”, “rect”, “arrowHead” or a custom image.

For Circles:
“r”: The radius, defined as a fraction of cell size. r=1 will

fill the entire cell.

“xAlign”, “yAlign”: Alignment of the circle within the cell.

Defaults to 0.5 (center).

For Rectangles:
“w”, “h”: The width and height of the rectangle, which are in

fractions of cell width and height.

“xAlign”, “yAlign”: Alignment of the rectangle within the

cell. Defaults to 0.5 (center).

For arrowHead:

“scale”: Proportion scaling as a fraction of cell size. “heading_x”: represents x direction unit vector. “heading_y”: represents y direction unit vector.

For an image:

The image must be placed in the same directory from which the server is launched. An image has the attributes “x”, “y”, “scale”, “text” and “text_color”.

“Color”: The color to draw the shape in; needs to be a valid HTML

color, e.g.”Red” or “#AA08F8”

“Filled”: either “true” or “false”, and determines whether the shape is

filled or not.

“Layer”: Layer number of 0 or above; higher-numbered layers are drawn

above lower-numbered layers.

“text”: The text to be inscribed inside the Shape. Normally useful for

showing the unique_id of the agent.

“text_color”: The color to draw the inscribed text. Should be given in

conjunction of “text” property.

Attributes:
portrayal_method: Function which generates portrayals from objects, as

described above.

grid_height, grid_width: Size of the grid to visualize, in cells. canvas_height, canvas_width: Size, in pixels, of the grid visualization

to draw on the client.

template: “canvas_module.html” stores the module’s HTML template.

Instantiate a new CanvasGrid.

Args:
portrayal_method: function to convert each object on the grid to

a portrayal, as described above.

grid_width, grid_height: Size of the grid, in cells. canvas_height, canvas_width: Size of the canvas to draw in the

client, in pixels. (default: 500x500)

package_includes = ['GridDraw.js', 'CanvasModule.js', 'InteractionHandler.js']
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.ChartVisualization module

Chart Module

Module for drawing live-updating line charts using Charts.js

class ChartModule(series, canvas_height=200, canvas_width=500, data_collector_name='datacollector')[source]

Bases: VisualizationElement

Each chart can visualize one or more model-level series as lines

with the data value on the Y axis and the step number as the X axis.

At the moment, each call to the render method returns a list of the most recent values of each series.

Attributes:
series: A list of dictionaries containing information on series to

plot. Each dictionary must contain (at least) the “Label” and “Color” keys. The “Label” value must correspond to a model-level series collected by the model’s DataCollector, and “Color” must have a valid HTML color.

canvas_height, canvas_width: The width and height to draw the chart on

the page, in pixels. Default to 200 x 500

data_collector_name: Name of the DataCollector object in the model to

retrieve data from.

template: “chart_module.html” stores the HTML template for the module.

Example:
schelling_chart = ChartModule([{“Label”: “happy”, “Color”: “Black”}],

data_collector_name=”datacollector”)

TODO:

Have it be able to handle agent-level variables as well.

More Pythonic customization; in particular, have both series-level and chart-level options settable in Python, and passed to the front-end the same way that “Color” is currently.

Create a new line chart visualization.

Args:
series: A list of dictionaries containing series names and

HTML colors to chart them in, e.g. [{“Label”: “happy”, “Color”: “Black”},]

canvas_height, canvas_width: Size in pixels of the chart to draw. data_collector_name: Name of the DataCollector to use.

package_includes = ['external/chart-3.6.1.min.js', 'ChartModule.js']
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.HexGridVisualization module

Modular Canvas Rendering

Module for visualizing model objects in hexagonal grid cells.

class CanvasHexGrid(portrayal_method, grid_width, grid_height, canvas_width=500, canvas_height=500)[source]

Bases: VisualizationElement

A CanvasHexGrid object functions similarly to a CanvasGrid object. It takes a portrayal dictionary and talks to HexDraw.js to draw that shape.

A portrayal as a dictionary with the following structure:

“x”, “y”: Coordinates for the cell in which the object is placed. “Shape”: Can be either “hex” or “circle”

“r”: The radius, defined as a fraction of cell size. r=1 will

fill the entire cell.

“Color”: The color to draw the shape in; needs to be a valid HTML

color, e.g.”Red” or “#AA08F8”

“Filled”: either “true” or “false”, and determines whether the shape is

filled or not.

“Layer”: Layer number of 0 or above; higher-numbered layers are drawn

above lower-numbered layers.

“text”: The text to be inscribed inside the Shape. Normally useful for

showing the unique_id of the agent.

“text_color”: The color to draw the inscribed text. Should be given in

conjunction of “text” property.

Attributes:
portrayal_method: Function which generates portrayals from objects, as

described above.

grid_height, grid_width: Size of the grid to visualize, in cells. canvas_height, canvas_width: Size, in pixels, of the grid visualization

to draw on the client.

template: “canvas_module.html” stores the module’s HTML template.

Instantiate a new CanvasGrid.

Args:
portrayal_method: function to convert each object on the grid to

a portrayal, as described above.

grid_width, grid_height: Size of the grid, in cells. canvas_height, canvas_width: Size of the canvas to draw in the

client, in pixels. (default: 500x500)

package_includes = ['HexDraw.js', 'CanvasHexModule.js', 'InteractionHandler.js']
portrayal_method = None
canvas_width = 500
canvas_height = 500
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.NetworkVisualization module

Network Visualization Module

Module for rendering the network, using [d3.js](https://d3js.org/) framework.

class NetworkModule(portrayal_method, canvas_height=500, canvas_width=500)[source]

Bases: VisualizationElement

package_includes = []
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.PieChartVisualization module

Pie Chart Module

Module for drawing live-updating pie charts using d3.js

class PieChartModule(fields, canvas_height=500, canvas_width=500, data_collector_name='datacollector')[source]

Bases: VisualizationElement

Each chart can visualize one set of fields from a datacollector as a pie chart.

Attributes:
fields: A list of dictionaries containing information on fields to

plot. Each dictionary must contain (at least) the “Label” and “Color” keys. The “Label” value must correspond to a model-level field collected by the model’s DataCollector, and “Color” must have a valid HTML color.

canvas_height, canvas_width: The width and height to draw the chart on

the page, in pixels. Default to 500 x 500

data_collector_name: Name of the DataCollector object in the model to

retrieve data from.

Create a new line chart visualization.

Args:
fields: A list of dictionaries containing fields names and

HTML colors to chart them in, e.g. [{“Label”: “happy”, “Color”: “Black”},]

canvas_height, canvas_width: Size in pixels of the chart to draw. data_collector_name: Name of the DataCollector to use.

package_includes = ['external/d3-7.4.3.min.js', 'PieChartModule.js']
render(model)[source]

Build visualization data from a model object.

Args:

model: A model object

Returns:

A JSON-ready object.

mesa.visualization.modules.TextVisualization module

Module contents

Container for all built-in visualization modules.