Skip to content

Conversation

timhoffm
Copy link
Member

Replace default initialization immediately followed by setting values by initialization with values, i.e.

# before:
fp = FontProperties()
fp.set_[something](val)

# after
fp = FontProperties([something]=val)

This is clearer and additionally helps with the possible transition of making FontProperties immutable, see #22495.

@github-actions github-actions bot added Documentation: examples files in galleries/examples Documentation: user guide files in galleries/users_explain or doc/users labels Sep 18, 2024
@@ -21,43 +21,36 @@
fig.text(0.1, 0.9, 'family', fontproperties=heading_font, **alignment)
families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']
for k, family in enumerate(families):
font = FontProperties()
font.set_family(family)
font = FontProperties(family=family)
Copy link
Member

@QuLogic QuLogic Sep 18, 2024

Choose a reason for hiding this comment

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

This broke with sans-serif, probably because of this special case:
https://github.com/QuLogic/matplotlib/blob/0cdb45da1bfe0af330f87eeadcf633919cd26f80/lib/matplotlib/font_manager.py#L614-L620
It could probably be fixed by passing family=[family].

Copy link
Member Author

@timhoffm timhoffm Sep 18, 2024

Choose a reason for hiding this comment

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

Whoops. That's surprising and awkward. The __init__ parameter behaves differently compared to the set_family method. 😢 But it's documented.

I believe FontProperties(pattern) should be deprecated and we should instead add a FontProperties.from_pattern(pattern) method. But that's for another PR (see #28837). For now, we have to go with the list.

Replace default initialization immediately followed by setting values
by initialization with values, i.e.

```
# before:
fp = FontProperties()
fp.set_[something](val)

# after
fp = FontProperties([something]=val)
```

This is clearer and additionally helps with the possible transition
of making FontProperties immutable, see matplotlib#22495.
@QuLogic QuLogic added this to the v3.9.3 milestone Sep 19, 2024
@QuLogic QuLogic merged commit 4b35874 into matplotlib:main Sep 19, 2024
22 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Sep 19, 2024
@timhoffm timhoffm deleted the FontProperties-init branch January 18, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples Documentation: user guide files in galleries/users_explain or doc/users Maintenance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants