mesa / mesa/mesa-examples

Improve boid flockers (Solara) visualisation with direction and colors

Open
#86 7 comments 0 reactions 1 assignee Claimed by @rht View on GitHub
good first issue
Dominant language
Python
Stars
252
Forks
279
Avg merge
8d 9h
Merged PRs (30d)
2

Description

The boid flockers example can be improved with (Solara) visualisation that includes the direction and colors for the boids. It would be nice to have an arrow-like shape to know the direction, and either random colors or to let them take the colors of a group/leader.

Here's an example, in matplotlib, from an old exam we used:

![Screenshot_351](https://github.com/projectmesa/mesa-examples/assets/15776622/19816294-d420-424b-be72-7f54e7870f2f)

```Python
# set the colormap we will use for plotting
bwr = plt.get_cmap("gist_rainbow")

def draw_boids(model):
x_vals = []
y_vals = []
u_vals = []
v_vals = []
colors = [] # List to hold the color for each agent

# Generate unique colors with saturation and transparency
# Use a colormap with high saturation, such as "hsv"
cmap = plt.get_cmap("hsv")
n_agents = len(model.schedule.agents)
color_indices = np.linspace(0, 1, n_agents)

for i, boid in enumerate(model.schedule.agents):
x, y = boid.pos
u, v = boid.direction_vector
x_vals.append(x)
y_vals.append(y)
u_vals.append(u)
v_vals.append(v)

# Assign a color with transparency; alpha value less than 1 introduces transparency
colors.append(cmap(color_indices[i])[:3] + (0.6,)) # Change 0.6 to the desired transparency

fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111)

# Use quiver with colors
q = ax.quiver(x_vals, y_vals, u_vals, v_vals, color=colors, angles='xy', pivot='mid',
headaxislength=8, headlength=10, headwidth=7, scale=35)

# The call to `embed_identity` is not changed.
embed_identity(ax, student_name, student_number)

draw_boids(model)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.