Description
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:
- Values are not discoverable without reading the docs
- Values do not autocomplete and cannot be checked for correctness by most IDEs
- 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.