You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[MNT]: Collect style properties in a dedicated object #30828
Currently, style information is directly attached to the Artist classes, i.e. mixed with data, metadata (e.g. gid, label) and drawing information (e.g. animated, zorder).
Proposal
I prose to collect the style information in a dedicated container, e.g. a dataclass, that will be a member of the Artist.
Feedback welcome!
Motivation
Similar how the data prototype collects and abstracts data handling, an ArtistStyle collects all visual aspects of the Artist. This creates internal semantic structures: While Artists currently contain a long ist of artist properties with various purposes, we may simplify them by grouping properties by concerns. I anticipate that this will make it easier to reason about them. Also this can enable new features like multiple artists sharing styles, or dynamic style updates.
Additional information
For a start, this would be an internal change, without any modification to the public Artist API.
As an example, I could see the following Line2D properties grouped into a style:
I'm roughly for separating out the styling from the behavior b/c I think it's a prerequisite to serialization (it's also loosely tied to mep 26) and cleaning up the style sheets/style sheets docs. There was a bit of a discussion on one of the calls about protocols vs classes that might also apply here.
When I've looked into this, roughly the parameter families map to the mark types/base artist classes - point (unfilled marker), line (Line2D), area (Patch, which contains Line2D for edge) - which I think can be roughly specified as protocols/classes & then any artist specific styling can be specified as an add on. Making the composition explicit would also probably make it easier to sort out which style parameters can be shared.
Attn @ksunden b/c a good chunk of the prototype work is new artists.
I have opened a Draft PR (#30832) with a proof-of-concept implementation for this proposal.
In the prototype, I introduced a LineStyle dataclass within lines.py and refactored Line2D to store the linewidth property inside this style object instead of directly on the instance (self._style.linewidth vs self._linewidth).
This demonstrates that we can group style properties internally while maintaining full backward compatibility with the existing public API (getters/setters). I would appreciate any feedback on this approach before expanding it to other properties.
@DipayanDasgupta thanks for your interest in contributing. However, you're jumping to action way too quickly. That one can internally group attributes into a dataclass is clear and does not need a demonstration.
The first question is whether we want to follow this approach, and if so, how exactly (c.f. @story645's post). The main task here is architecture, and that requires carefully analyzing the topic and making the right design decisions. Implementation itself will be fairly straight forward, once we know exactly what we want.
Thanks for the course correction, @timhoffm . I took a step back to analyze the architectural implications here, particularly regarding shared state and serialization as mentioned by @story645 . If the goal is enabling multiple artists to share styles with dynamic updates, the primary challenge isn't really the storage itself but rather the cache invalidation or observability. Since the current implementation immediately sets the stale flag on the artist when a property changes, moving state to a passive dataclass means we lose that trigger. To support shared styles where modifying one object updates multiple artists, we essentially need an observer pattern.
I think the robust path forward combines composition with an active observer model. Building on the earlier point about protocols, instead of a monolithic style object, we could compose artists from semantic units like a StrokeStyle for lines and edges or a FillStyle for patches. This creates a reusable schema where a Line2D holds a StrokeStyle and a Rectangle holds both a StrokeStyle and a FillStyle. To handle the updates without memory leaks, these style containers could implement a lightweight publisher-subscriber model using weak references to notify registered artists when a value changes. We would also need to decide if the style object holds strict explicit values or fully resolved values from rcParams. If this aligns with the vision, the first step would be defining that class structure and invalidation mechanism rather than refactoring the specific artists immediately.
@DipayanDasgupta while I appreciate your enthusiasm, I really think @ksunden needs to chime in as he's been working on new artists for a couple of years now.
Note to self: Overall one can classify the state of an artist (i.e. contents of self.__dict__) into the following categories:
style properties
other poperties, e.g. pick_radius, mouseover, url, gid
data: xdata, ydata
internal state
Idea: Stucture / group the state:
data moves to the data container
style moves to a dedicated object (separate appearance from contents - conceptually similar to CSS)
for now we are left with a zoo of remaining state in self.__dict__ - it may be possible to split another concept from it, but there are border cases so that a complete partition into topics may be difficult. Let's not do too much at a time.
Artist properties set_* are need for categories 1-3 (i.e. everything except internal state). They remain the public interface and should dispatch internally to the respective state; e.g. set_linestyle will go to self._style.linestyle instead of self._linestyle.
Activity
story645 commentedon Dec 9, 2025
I'm roughly for separating out the styling from the behavior b/c I think it's a prerequisite to serialization (it's also loosely tied to mep 26) and cleaning up the style sheets/style sheets docs. There was a bit of a discussion on one of the calls about protocols vs classes that might also apply here.
When I've looked into this, roughly the parameter families map to the mark types/base artist classes - point (unfilled marker), line (Line2D), area (Patch, which contains Line2D for edge) - which I think can be roughly specified as protocols/classes & then any artist specific styling can be specified as an add on. Making the composition explicit would also probably make it easier to sort out which style parameters can be shared.
Attn @ksunden b/c a good chunk of the prototype work is new artists.
MNT: Prototype LineStyle object for Line2D (matplotlib#30828)
DipayanDasgupta commentedon Dec 10, 2025
Hi @timhoffm,
I have opened a Draft PR (#30832) with a proof-of-concept implementation for this proposal.
In the prototype, I introduced a
LineStyledataclass withinlines.pyand refactoredLine2Dto store thelinewidthproperty inside this style object instead of directly on the instance (self._style.linewidthvsself._linewidth).This demonstrates that we can group style properties internally while maintaining full backward compatibility with the existing public API (getters/setters). I would appreciate any feedback on this approach before expanding it to other properties.
MNT: Prototype _LineStyle object for Line2D (matplotlib#30828)
timhoffm commentedon Dec 10, 2025
@DipayanDasgupta thanks for your interest in contributing. However, you're jumping to action way too quickly. That one can internally group attributes into a dataclass is clear and does not need a demonstration.
The first question is whether we want to follow this approach, and if so, how exactly (c.f. @story645's post). The main task here is architecture, and that requires carefully analyzing the topic and making the right design decisions. Implementation itself will be fairly straight forward, once we know exactly what we want.
DipayanDasgupta commentedon Dec 10, 2025
Thanks for the course correction, @timhoffm . I took a step back to analyze the architectural implications here, particularly regarding shared state and serialization as mentioned by @story645 . If the goal is enabling multiple artists to share styles with dynamic updates, the primary challenge isn't really the storage itself but rather the cache invalidation or observability. Since the current implementation immediately sets the stale flag on the artist when a property changes, moving state to a passive dataclass means we lose that trigger. To support shared styles where modifying one object updates multiple artists, we essentially need an observer pattern.
I think the robust path forward combines composition with an active observer model. Building on the earlier point about protocols, instead of a monolithic style object, we could compose artists from semantic units like a StrokeStyle for lines and edges or a FillStyle for patches. This creates a reusable schema where a Line2D holds a StrokeStyle and a Rectangle holds both a StrokeStyle and a FillStyle. To handle the updates without memory leaks, these style containers could implement a lightweight publisher-subscriber model using weak references to notify registered artists when a value changes. We would also need to decide if the style object holds strict explicit values or fully resolved values from rcParams. If this aligns with the vision, the first step would be defining that class structure and invalidation mechanism rather than refactoring the specific artists immediately.
story645 commentedon Dec 10, 2025
@DipayanDasgupta while I appreciate your enthusiasm, I really think @ksunden needs to chime in as he's been working on new artists for a couple of years now.
timhoffm commentedon Jan 26, 2026
Note to self: Overall one can classify the state of an artist (i.e. contents of
self.__dict__) into the following categories:pick_radius,mouseover,url,gidxdata,ydataIdea: Stucture / group the state:
self.__dict__- it may be possible to split another concept from it, but there are border cases so that a complete partition into topics may be difficult. Let's not do too much at a time.Artist properties
set_*are need for categories 1-3 (i.e. everything except internal state). They remain the public interface and should dispatch internally to the respective state; e.g.set_linestylewill go toself._style.linestyleinstead ofself._linestyle.