Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced customization options for agent visualization #2341

Closed
wants to merge 1 commit into from

Conversation

EwoutH
Copy link
Member

@EwoutH EwoutH commented Oct 4, 2024

This PR builds on #2242 by @rmhopkins4.

This allows more visualisation options for agents, by allowing specifying edgecolor, linewidth and alpha in the agent_portrayal.

Implementation

  • Refactored the _get_agent_data function to handle new visualization parameters (edge colors, line widths, alpha).
  • Implemented backward compatibility for old keyword arguments using a translation dictionary.
  • Updated _draw_continuous_space and _draw_voronoi functions to use the new agent data format.
  • Improved code reusability by centralizing agent data retrieval logic.
  • Ensured consistency in visualization across different space types (grid, continuous, Voronoi).

Usage Examples

Users can now specify additional properties in their agent portrayal function. For example, display agents with red fill, black edges, and partial transparency.

agent_portrayal = {
        "shape": "^",
        "color": "red",
        "size": 20,
        "edgecolor": "black",
        "linewidth": 2,
        "alpha": 0.7
    }

image

Another example, dependent on the state of the Agent:

def agent_portrayal(agent):
    return {
        "c": "white" if agent.isAlive else "black",
        "s": 45,
        "marker": "*",
        "edgecolors": "purple",
        "linewidths": 0.5,
        "alpha": 0.5,
    }

image

Additional notes

  • This change maintains backward compatibility with existing agent portrayal implementations.
  • The new visualization options are available for all space types (grid, continuous, Voronoi).
  • Future work could include extending these options to network visualizations and adding more advanced styling capabilities.

@EwoutH EwoutH added the enhancement Release notes label label Oct 4, 2024
@EwoutH EwoutH requested a review from Corvince October 4, 2024 11:28

This comment was marked as off-topic.

@quaquel
Copy link
Member

quaquel commented Oct 4, 2024

I like the API idea a lot. No time to review in detail atm.

- Add support for edgecolors, linewidths, and alpha transparency in agent portrayal
- Implement backward compatibility for old keyword arguments
- Refactor _get_agent_data function to handle new visualization parameters
- Update _draw_continuous_space and _draw_voronoi functions to use new agent data format
- Improve code reusability and consistency across different space types

Co-Authored-By: Robert Hopkins <[email protected]>
@EwoutH
Copy link
Member Author

EwoutH commented Oct 5, 2024

I have to fix a few small things, but API wise it's ready. I rebased the PR on the latest main.

@EwoutH EwoutH mentioned this pull request Oct 5, 2024
8 tasks
Copy link
Contributor

@Corvince Corvince left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I somehow missed this PR. Less code, more options sounds good!

@EwoutH
Copy link
Member Author

EwoutH commented Oct 17, 2024

Thanks!

Only now I completely forgot what small things I still needed to fix 😅.


return out
"""Draw agents in a Voronoi space."""
agent_data = _get_agent_data(space, agent_portrayal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. This method is for plotting the experimental Voronoi space.

The _get_agent_data method presupposes an old style space because it does for agents, pos in space.coord_iter():.

Copy link
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my remark about how this changes _draw_voronoi (which is a new style experimental space).

@quaquel
Copy link
Member

quaquel commented Nov 7, 2024

@EwoutH, with all the refactoring, how do you want to proceed with this? I had a quick look and have some ideas on how to provide this functionality within the refactored code-base. The tricky part is that linewidth and edge color can be provided at the agent level (currently not supported) or at the level of a single ax.scatter command (currently already supported). alpha is already supported via some **kwargs magic.

@EwoutH
Copy link
Member Author

EwoutH commented Nov 7, 2024

alpha was by far the most important in this PR, the rest was "bijvangst". So maybe we can document how alpha works (or support it better if needed), and leave the rest be for now.

I'm not attached to this PR at all.

@quaquel
Copy link
Member

quaquel commented Nov 8, 2024

alpha was by far the most important in this PR, the rest was "bijvangst". So maybe we can document how alpha works (or support it better if needed), and leave the rest be for NWO .

To be clear: support for alpha did not exist before #2430, which added **space_drawing_kwargs. You can now do draw_space(model.grid, my_agent_portrayal, alpha=0.5) and then all ax.scatter calls will use an alpha of 0.5. What is currently not supported is using alpha for each agent separately. According to the docs of matplotlib, this should not even work... but it does.

The ax.scatter docs say:

alpha: float, default: None

but alpha array-like of float also works. I'll put in a new PR to support this.

@EwoutH
Copy link
Member Author

EwoutH commented Nov 8, 2024

Interesting, notified them upstream:

@quaquel
Copy link
Member

quaquel commented Nov 8, 2024

superseded by #2468

@quaquel quaquel closed this Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants