Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[feature request] Use Enums for enumerated types #14642

Copy link
Copy link
Open
@clbarnes

Description

@clbarnes
Issue body actions

Matplotlib uses enumerated types, or something like them, in a number of places: for example, when setting locations ("upper", "lower" etc.). Historically, the pattern here has been to use strings. However, this is undesirable:

  1. Values are not discoverable without reading the docs
  2. Values do not autocomplete and cannot be checked for correctness by most IDEs
  3. Remembering the canonical dashes, underscores and capitals is unnecessarily complicated

Instead, we could use an enum.Enum, which solves 1 and 2, and renders 3 moot.

By using an enum which also subclasses str, existing code would not break, and people could still pass in strings if they wanted to. No new code would be required in functions which take these strings; they'd just need to be defined once.

Alternatively, for e.g. locations, an IntFlag could be used so that "upper right" could be Location.UPPER & Location.RIGHT (this would probably require some internal changes). No more need to remember which way round the specifiers go, and whether it's "upper" or "top": my IDE knows so I don't have to (and nor does anyone coming to the concept for the first time).

One use case is for named colors. It would be trivial for IDEs to tell me what named colors are available, and check my spelling, but as it is, I'm googling the docs page every time I need them. I made a trivial package which does this: https://github.com/clbarnes/mpl_colors . The enum instances are also instances of an RGB namedtuple, which is also convenient for stacking them into an array, converting them into other spaces, comparing them to other color tuples, and so on.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.