FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

added property usemathtext to EngFormatter by pharshalp · Pull Request #12856 · matplotlib/matplotlib · GitHub

Repository navigation

added property usemathtext to EngFormatter - #12856

Merged
timhoffm merged 8 commits into
matplotlib:masterfrom
pharshalp:engformatter_tex_fix
Mar 3, 2019
Merged

timhoffm merged 8 commits into
matplotlib:masterfrom
pharshalp:engformatter_tex_fix

Conversation

pharshalp commented Nov 21, 2018 •
edited
Loading

Copy link
Copy Markdown
Contributor

PR Summary

Based on #12847 (comment)

Adding ._usetex as an attribute for EngFormatter so as to allow the user to set it to False (resulting in numbers being displayed using text font instead of math font). This will be consistent with what ScalarFormatter allows.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

jklymak commented Nov 21, 2018

Copy link
Copy Markdown
Member

We use the convention that underscore attributes are private. So no one should be accessing that attribute, so I don’t think you should add it until we provide a public interface.

pharshalp changed the title workaround to allow text font for numbers in EngFormatter when used with tex added property usemathtext to EngFormatter Nov 21, 2018

Copy link
Copy Markdown
Contributor Author

following in the footsteps of ScalarFormatter, added a public interface (i.e. the property usemathtext).

ImportanceOfBeingErnest commented Nov 21, 2018 •
edited
Loading

Copy link
Copy Markdown
Member

This comment is written with respect to the first commit here.

This would at least allow to work around the issue by

plt.rcParams["text.usetex"] = False
plt.gca().xaxis.set_major_formatter(EngFormatter())
plt.rcParams["text.usetex"] = True

So even if the argument is that "private" should not be used by anyone, this is still a useful change.
Note that the above temporary rcparams change does currently not work reliably for ScalarFormatter where people need to use the "private" thingy to not have mathmode turned on for labels.

Also, I'm not actually convinced that the general approach of #12847 is useful for the EngFormatter

"${mant:{fmt}}${sep}{prefix}"

This will now lead to the numbers being rendered in a different font than the unit prefixes. I can hardly imagine that this is what people want by default, and it's especially annoying if there isn't even any workaround for it.

pharshalp commented Nov 21, 2018 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

I agree with both the views presented here. I think I have stepped into something which is far beyond my reach... I will wait for all the experienced core developers to weigh-in before making any changes. I will be happy to implement the suggestions on how to move forward. Please feel free to take this over if it would be easier that way.

Copy link
Copy Markdown
Member

This comment is written with respect to the second commit here.

This goes in the right direction IMO. But... the useMathText argument is now interpreted inconsistently between ScalarFormatter and EngFormatter. The problem is also that the notion "MathText" explicitely means the unicode rendering used outside of tex.

Maybe some more bikeshedding is needed here?

pharshalp commented Nov 21, 2018 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Would the following approach work?

(1) not treating usetex and usemathtext as being equivalent inside ScalarFormatter (as suggested in #12847 (comment))
(2) Adding a new property (say useTeXMath with useTeXMath=False as the default) for formatters that allows encapsulating numbers with $$ signs -- this will allow the user to set numbers in math font and suffixes in text font in case of EngFormatter (if they want to).

Copy link
Copy Markdown
Member

Possibly... I'm not having the complete overview of ScalarFormatter currently - but it's a total mess. So once you start changing that, the rabbit hole will soon become even deeper. In fact, ._usetex and useMathText are currently mutually exclusive in ScalarFormatter.
Having a useMathText and a useTexMath argument simultaneously is sure confusing though.

As of now I think I would be happy with either of (a) leaving EngFormatter untouched, i.e. revert #12847, (b) Get the first commit here in, in case people think that the different fonts for numbers and unit prefix are acceptable.

pharshalp commented Nov 21, 2018 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

out of those two options I think I would prefer to go with (b) since that allows for the most flexible behavior (for now). I will wait for comments from other core/experienced developers before proceeding. Ideally I would like to fix ScalarFormatter and "go down the rabbit hole" but I do not have sufficient expertise to do that.

Copy link
Copy Markdown
Contributor Author

should I proceed by reverting the change in #12847? I would appreciate some advice from core/experienced developers. Thanks!

tacaswell added this to the v3.1.0 milestone Feb 21, 2019

Copy link
Copy Markdown
Member

(sorry started working on this yesterday, and got distracted)

Well, this is a fun pickle of inconsistent APIs. To check my understanding:

  • Currently with ScalarFromatter detects if rcParams['text.usetex'] is True and if so uses $$ around numbers
  • this is not universally desired behavior and there is a way (by poking a private attribute!) way to opt-out of this
  • previously EngFormatter did not behave the same (and never used $$)
  • correctly format ticklabels when EngFormatter is used with usetex = True #12847 made EngFormatter unconditionally use $$ if rcParams['text.usetex'] is True to make it consistent with ScalarFormatter
  • it was pointed out that ScalarFormatter has the cut out and this PR is putting that cutout in EngFormatter too
  • adding a private attribute as a control was pointed out as an anti-pattern
  • now we are in a discussion about adding a public API to control this

Did I get the facts/history right?

Copy link
Copy Markdown
Contributor Author

Yes!

Copy link
Copy Markdown
Member

May I just add that allowing for the ._usetex attribute is not only about allowing users to bypass the public API, but would also simplify any future work on those formatters by using the same internal structure.
So even if the discussion about adding a public API to control this is not happening right now, or takes too long, one would still leave the code in a state that is easier to pick up from in the future.

anntzer commented Feb 22, 2019

Copy link
Copy Markdown
Contributor

I think(??) the end target would be to have

  • Formatter.{get,set}_usetex(); the default value is None which the setter converts eagerly to rcParams["text.usetex"] (consistent with Text.{get,set}_usetex);
  • Formatter.{get,set}_useMathText() (if possible with a less atrocious name but that's what we have right now); the default value is None which the setter converts eagerly to rcParams["axes.formatter.get_usemathtext"]

?

Copy link
Copy Markdown
Member

Seems correct; not to forget to have those arguments settable in the init.

Btw. relevant issue here #8436, #11718, #11586

Copy link
Copy Markdown
Contributor Author

Thanks all for the feedback. I will implement the suggestions.

pharshalp changed the title added property usemathtext to EngFormatter [WIP] added property usemathtext to EngFormatter Feb 24, 2019
pharshalp changed the title [WIP] added property usemathtext to EngFormatter added property usemathtext to EngFormatter Feb 24, 2019

Copy link
Copy Markdown
Contributor Author

I have made the changes as suggested. Please note that I have assumed an equivalence between usetex and useMathText when inserting $ signs around numbers (to be consistent with the ScalarFormatter and based on the comment here).

A positive implication of this decision:
If there are negative numbers present (when using useMathText=True), the unicode minus sign will be used (without relying on fix_minus). This will be consistent with the notion that useMathText implies unicode rendering (as mentioned in one of the comments here).

Comment thread lib/matplotlib/ticker.py Outdated
}

def __init__(self, unit="", places=None, sep=" "):
def __init__(self, unit="", places=None, sep=" ", usetex=None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please make the new arguments keyword-only (sep=" ", *, usetex=None, ...).

Comment thread lib/matplotlib/ticker.py Outdated
* ``sep="\\N{THIN SPACE}"`` (``U+2009``);
* ``sep="\\N{NARROW NO-BREAK SPACE}"`` (``U+202F``);
* ``sep="\\N{NO-BREAK SPACE}"`` (``U+00A0``).
`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

the extra, spurious backtick here makes the doc build unhappy

anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

modulo CI.

Copy link
Copy Markdown
Member

I am glad this came to a conclusion!

Does this same public API also exist on ScalerFormatter?

Could you please add a whats_new entry?

Copy link
Copy Markdown
Contributor Author

Added a what's new entry.

I don't think there is a public API for controling the usetex behavior in ScalarFormatter. I can open a new PR to add that if this is ok with the core developers.

Thank you everyone involved in this discussion for guiding me through this!

timhoffm merged commit 684a1ea into matplotlib:master Mar 3, 2019

timhoffm commented Mar 3, 2019

Copy link
Copy Markdown
Member

Thanks!

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 3, 2019
timhoffm added a commit that referenced this pull request Mar 3, 2019
…856-on-v3.1.x

Backport PR #12856 on branch v3.1.x (added property  usemathtext to EngFormatter)
pharshalp deleted the engformatter_tex_fix branch March 3, 2019 07:37
pharshalp restored the engformatter_tex_fix branch April 29, 2019 01:43
pharshalp deleted the engformatter_tex_fix branch January 27, 2020 17:05

Copy link
Copy Markdown

@pharshalp Did you end up adding a public API for usetex in ScalarFormatter?

Copy link
Copy Markdown

If not, I might be able to take a crack at it over winter break.

Copy link
Copy Markdown
Contributor Author

@mattdutson I didn't end up adding a public API for usetex in ScalarFormatter (since I didn't hear from any of the the core developers -- quoting my response relevant to this).

Added a what's new entry.

I don't think there is a public API for controling the usetex behavior in ScalarFormatter. I can open a new PR to add that if this is ok with the core developers.

Thank you everyone involved in this discussion for guiding me through this!

Please feel free to take a crack at it. Thanks!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants


Back | FazBrowse Home | New Git URL