[Matplotlib-devel] Easier tick formatters

Antony Lee antony.lee at institutoptique.fr
Thu Mar 5 05:37:13 EST 2020


I am sympathetic to the idea.  A much simpler rule, though, would be to
*just* additionally accept callables.  New-style format string formatters
can then be implemented by passing bound methods
(`set_major_formatter("tick {}".format)` -- the bound method quietly
ignores the second (index) argument), which also makes it explicit that we
talking about {}-format, not %s-format.  Even
`set_major_formatter("".format)` would already be much shorter to type than
importing NullFormatter and using it.  In any case, we could always first
implement support for callables, which already buys us all these cases, and
defer further cases to later.
The only case not covered by callables/bound methods is FixedLocator, but
this is already handled by set_x/yticks, and anyways (usually) involves
setting both the locator and the formatter at once (and in fact is
semantically more associated with setting the locator rather than the
formatter), so I think it's fine to keep it separate for now.
Antony

On Thu, Mar 5, 2020 at 4:03 AM Todd <toddrjen at gmail.com> wrote:

> Currently setting up the tick formatting on matplotlib requires you to
> import a formatter from the ticker module, create an instance of that
> formatter, then pass that to the axis.  This makes sense for complicated
> formatting.  However, there are some obvious cases where we can probably
> infer the type of formatting someone wants to do if we are just given the
> formatter content.  For example
>
>    * '' is probably going to be the null formatter
>    * A callable, besides a formatter instance, is probably going to be a
> function formatter
>    * A sequence or numpy array is probably going to be an index or fixed
> formatter, depending on the locator
>    * Any other string is probably going to be a format string formatter or
> string method formatter.
>
> So I think we could allow the `set_major_formatter` and `set_minor_formatter`
> to take certain sorts of inputs and create the formatter automatically for
> the user, making such cases much, much easier.  Specifically, I propose the
> following inputs be accepted:
>
>    * `None` sets it to the default formatter
>    * A callable (besides a formatter instance) is set to be a FuncFormatter
>    * An empty string, `''`, is set to be a NullFormatter
>    * Any other string is a StrMethodFormatter
>    * An abc.collections.Sequence subclass or numpy ndarray subclass is an
> IndexFormatter, unless the axis is using a FixedLocator in which case it is
> a FixedLocator.  We could restrict this to just lists and numpy arrays only.
>
> Any thoughts?
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20200305/9ea7668a/attachment-0001.html>


More information about the Matplotlib-devel mailing list