Visualization
The dissmodel.visualization module provides graphical and interactive representations
of running simulations. All visualization components inherit from Model and are
therefore integrated into the simulation clock — they update automatically at each step.
Three main components are available:
| Component | Substrate | Description |
|---|---|---|
Chart |
Any | Time-series plots from tracked model variables |
Map |
Vector (GeoDataFrame) | Dynamic spatial maps updated each step |
RasterMap |
Raster (NumPy) | Raster array rendering — categorical or continuous |
All three support three output targets: local matplotlib window,
Jupyter inline display, and Streamlit st.empty() placeholder.
@track_plot
The track_plot decorator marks model attributes to be collected and plotted
by Chart. Each call defines the variable label, colour, and plot type.
from dissmodel.core import Model
from dissmodel.visualization import track_plot
@track_plot("Susceptible", "green")
@track_plot("Infected", "red")
@track_plot("Recovered", "blue")
class SIR(Model):
def setup(self, susceptible=9998, infected=2, recovered=0,
duration=2, contacts=6, probability=0.25):
self.susceptible = susceptible
self.infected = infected
self.recovered = recovered
self.duration = duration
self.contacts = contacts
self.probability = probability
def execute(self):
total = self.susceptible + self.infected + self.recovered
alpha = self.contacts * self.probability
new_inf = self.infected * alpha * (self.susceptible / total)
new_rec = self.infected / self.duration
self.susceptible -= new_inf
self.infected += new_inf - new_rec
self.recovered += new_rec
Chart
Displays time-series data from variables annotated with @track_plot.
from dissmodel.core import Environment
from dissmodel.models.sysdyn import SIR
from dissmodel.visualization import Chart
env = Environment(end_time=30)
SIR()
Chart(show_legend=True)
env.run()
Streamlit:
Chart(plot_area=st.empty())
Map
Renders spatial data from a GeoDataFrame, updated at every simulation step.
from dissmodel.visualization.map import Map
from matplotlib.colors import ListedColormap
Map(
gdf=gdf,
plot_params={
"column": "state",
"cmap": ListedColormap(["white", "black"]),
"ec": "gray",
},
)
RasterMap
Renders a named NumPy array from a RasterBackend. Supports categorical
(value → colour mapping) and continuous (colormap + colorbar) modes.
Categorical:
from dissmodel.visualization.raster_map import RasterMap
RasterMap(
backend = b,
band = "uso",
title = "Land Use",
color_map = {1: "#006400", 3: "#00008b", 5: "#d2b48c"},
labels = {1: "Mangrove", 3: "Sea", 5: "Bare soil"},
)
Continuous:
RasterMap(
backend = b,
band = "alt",
title = "Altimetry",
cmap = "terrain",
colorbar_label = "Altitude (m)",
mask_band = "uso",
mask_value = 3, # mask SEA cells
)
Headless (default when no display is available):
frames are saved to raster_map_frames/<band>_step_NNN.png.
display_inputs
Generates Streamlit input widgets automatically from a model's type annotations. Integer and float attributes become sliders; booleans become checkboxes.
from dissmodel.visualization import display_inputs
sir = SIR()
display_inputs(sir, st.sidebar)
Full Streamlit example
import streamlit as st
from dissmodel.core import Environment
from dissmodel.models.sysdyn import SIR
from dissmodel.visualization import Chart, display_inputs
st.set_page_config(page_title="SIR Model", layout="centered")
st.title("SIR Model — DisSModel")
st.sidebar.title("Parameters")
steps = st.sidebar.slider("Steps", min_value=1, max_value=50, value=10)
run_btn = st.button("Run")
env = Environment(end_time=steps, start_time=0)
sir = SIR()
display_inputs(sir, st.sidebar)
Chart(plot_area=st.empty())
if run_btn:
env.run()
API Reference
dissmodel.visualization.chart.Chart
Bases: Model
Simulation model that renders a live time-series chart.
Extends :class:~dissmodel.core.Model and redraws the chart at every
time step. Supports three rendering targets:
- Streamlit — pass a
plot_area(st.empty()). - Jupyter — detected automatically via :func:
is_notebook. - Matplotlib window — fallback for plain Python scripts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select
|
list of str
|
Subset of labels to plot. If |
required |
pause
|
bool
|
If |
required |
plot_area
|
any
|
Streamlit |
required |
show_legend
|
bool
|
Whether to display the plot legend, by default |
required |
show_grid
|
bool
|
Whether to display the plot grid, by default |
required |
title
|
str
|
Chart title, by default |
required |
Examples:
>>> env = Environment(end_time=30)
>>> Chart(show_legend=True, show_grid=True, title="SIR Model")
>>> env.run()
Source code in dissmodel/visualization/chart.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
execute()
Redraw the chart for the current simulation time step.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no interactive matplotlib backend is detected and the code is not running in a notebook or Streamlit context. |
Source code in dissmodel/visualization/chart.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
setup(select=None, pause=True, plot_area=None, show_legend=True, show_grid=False, title='Variable History')
Configure the chart.
Called automatically by salabim during component initialisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
select
|
list of str
|
Subset of labels to plot. If |
None
|
pause
|
bool
|
If |
True
|
plot_area
|
any
|
Streamlit |
None
|
show_legend
|
bool
|
Whether to display the plot legend, by default |
True
|
show_grid
|
bool
|
Whether to display the plot grid, by default |
False
|
title
|
str
|
Chart title, by default |
'Variable History'
|
Source code in dissmodel/visualization/chart.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
dissmodel.visualization.map.Map
Bases: Model
Simulation model that renders a live choropleth map of a GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame to render. |
required |
plot_params
|
dict
|
Keyword arguments forwarded to :meth: |
required |
figsize
|
tuple[int, int]
|
Figure size in inches. Default: |
required |
pause
|
bool
|
Call |
required |
interval
|
float
|
Seconds passed to |
required |
plot_area
|
empty() | None
|
Streamlit placeholder. Default: |
required |
save_frames
|
bool
|
Save one PNG per step to |
required |
Examples:
>>> env = Environment(end_time=10)
>>> Map(gdf=grid, plot_params={"column": "state", "cmap": "viridis"})
>>> env.run()
Source code in dissmodel/visualization/map.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
dissmodel.visualization.raster_map.RasterMap
Bases: Model
Visualization model for RasterBackend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
RasterBackend
|
|
required |
band
|
str
|
Array to visualize. |
required |
title
|
str
|
Figure title prefix. Default: |
required |
figsize
|
tuple[int, int]
|
Default: |
required |
pause
|
bool
|
Use |
required |
interval
|
float
|
Seconds between steps in interactive mode. Default: |
required |
plot_area
|
empty() | None
|
Streamlit placeholder. |
required |
auto_mask
|
bool
|
Apply the backend's extent mask automatically so pixels outside
the study area are transparent. Default: |
required |
Categorical mode (color_map provided)
color_map : dict[int, str]
{value: "#rrggbb"}
labels : dict[int, str]
{value: "label"} — used in the legend.
Continuous mode (color_map absent)
cmap : str
Matplotlib colormap name. Default: "viridis".
scheme : str
"manual" — use vmin / vmax (default).
"equal_interval" — divide [min, max] of valid data into k classes.
"quantiles" — p2–p98 of valid data, robust to outliers.
k : int
Number of colour classes for scheme="equal_interval". Default: 5.
vmin, vmax : float | None
Bounds for scheme="manual".
legend : bool
Show the colorbar. Default: True.
colorbar_label : str
Colorbar label. Default: band.
mask_band : str | None
Additional domain mask (e.g. mask sea cells for altimetry).
Applied on top of the automatic extent mask.
mask_value : int | float | None
Value in mask_band to mask.
Source code in dissmodel/visualization/raster_map.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |