8000 Clarify dollar_ticks example and FormatStrFormatter docs. by anntzer · Pull Request #27601 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Clarify dollar_ticks example and FormatStrFormatter docs. #27601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
8000
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion galleries/examples/ticks/dollar_ticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Dollar ticks
============

Use a `~.ticker.FormatStrFormatter` to prepend dollar signs on y-axis labels.
Use a format string to prepend dollar signs on y-axis labels.

.. redirect-from:: /gallery/pyplots/dollar_ticks
"""

import matplotlib.pyplot as plt
import numpy as np

Expand Down
16 changes: 13 additions & 3 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ class FormatStrFormatter(Formatter):
The format string should have a single variable format (%) in it.
It will be applied to the value (not the position) of the tick.

Negative numeric values will use a dash, not a Unicode minus; use mathtext
to get a Unicode minus by wrapping the format specifier with $ (e.g.
"$%g$").
Negative numeric values (e.g., -1) will use a dash, not a Unicode minus;
use mathtext to get a Unicode minus by wrapping the format specifier with $
(e.g. "$%g$").
"""

def __init__(self, fmt):
self.fmt = fmt

Expand All @@ -358,7 +359,16 @@ class StrMethodFormatter(Formatter):

The field used for the tick value must be labeled *x* and the field used
for the tick position must be labeled *pos*.

Negative numeric values (e.g., -1) will use a dash, not a Unicode minus;
use mathtext to get a Unicode minus by wrapping the format specifier with $
(e.g. "${x}$").
Comment on lines +363 to +365
Copy link
Member

Choose a reason for hiding this comment

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

why does this need to be here twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it applies both to FormatStrFormatter and to StrMethodFormatter. (Actually this may change with #27602...)

Copy link
Member

Choose a reason for hiding this comment

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

I hate github code folding sometimes. :/


It is typically unnecessary to explicitly construct `.StrMethodFormatter`
objects, as `~.Axis.set_major_formatter` directly accepts the format string
itself.
"""

def __init__(self, fmt):
self.fmt = fmt

Expand Down
0