-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[DOC] Tick locators & formatters examples #7084
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
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
32196ed
Add tick locator example
rougier 98618bd
Flakify
rougier e7ff52f
Added docstring description
rougier bb7a4d9
Added tick formatters example
rougier 015ed45
Changed the MaxN locator example to show the difference with Auto loc…
rougier bf5f5b2
Added index locator example
rougier b9e7c53
Typo
rougier 729c83b
Small change in the multiple locator
rougier 4b0b1f2
Add arguments to text
rougier c827e5a
Changed index locator range
rougier 6014664
Added function arguments to text describing the formatter
rougier 6a3a5b8
Added function arguments to text describing the formatter
rougier 9cbbc47
Flakify
rougier db0d321
Removed figure saving
rougier 79acce0
Cosmetic changes
rougier 6a157ae
Cosmetic changes
rougier e91a577
Removed line for saving figures
rougier 0875843
Removed unused function
rougier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tick formatters example
- Loading branch information
commit bb7a4d9a1a8d5dec77ed3123e14f3b5e8c26bdca
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Show the different tick formatters | ||
""" | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import matplotlib.ticker as ticker | ||
|
||
|
||
# Setup a plot such that only the bottom spine is shown | ||
def setup(ax): | ||
ax.spines['right'].set_color('none') | ||
ax.spines['left'].set_color('none') | ||
ax.yaxis.set_major_locator(ticker.NullLocator()) | ||
ax.spines['top'].set_color('none') | ||
ax.xaxis.set_ticks_position('bottom') | ||
ax.tick_params(which='major', width=1.00, length=5) | ||
ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) | ||
ax.set_xlim(0, 5) | ||
ax.set_ylim(0, 1) | ||
ax.patch.set_alpha(0.0) | ||
|
||
|
||
plt.figure(figsize=(8, 6)) | ||
n = 6 | ||
|
||
# Null formatter | ||
ax = plt.subplot(n, 1, 1) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
ax.xaxis.set_major_formatter(ticker.NullFormatter()) | ||
ax.xaxis.set_minor_formatter(ticker.NullFormatter()) | ||
ax.text(0.0, 0.5, "Null formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
# Fixed formatter | ||
ax = plt.subplot(n, 1, 2) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0)) | ||
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
majors = ["", "0", "1", "2", "3", "4", "5"] | ||
ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) | ||
minors = [""] + ["%.2f" % (x-int(x)) if (x-int(x)) | ||
else "" for x in np.arange(0, 5, 0.25)] | ||
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(minors)) | ||
ax.text(0.0, 0.5, "Fixed formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
|
||
# Func formatter | ||
def major_formatter(x, pos): | ||
return "[%.2f]" % x | ||
|
||
|
||
def minor_formatter(x, pos): | ||
return "(%.2f)" % x | ||
|
||
|
||
ax = plt.subplot(n, 1, 3) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
ax.xaxis.set_major_formatter(ticker.FuncFormatter(major_formatter)) | ||
ax.text(0.0, 0.5, "Func formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
|
||
# FormatStr formatter | ||
ax = plt.subplot(n, 1, 4) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) | ||
ax.text(0.0, 0.5, "FormatStr formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
# Scalar formatter | ||
ax = plt.subplot(n, 1, 5) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.AutoLocator()) | ||
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) | ||
ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) | ||
ax.text(0.0, 0.5, "Scalar formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
# StrMethod formatter | ||
ax = plt.subplot(n, 1, 6) | ||
setup(ax) | ||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) | ||
ax.text(0.0, 0.5, "StrMethod formatter", fontsize=16, transform=ax.transAxes) | ||
|
||
|
||
plt.tight_layout() | ||
# plt.savefig("tick-formatters.pdf") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We won't need these. |
||
# plt.savefig("tick-formatters.png", dpi=150) | ||
plt.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit confusing, not being the default, but maybe it should be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you mean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean it might be confusing for users to see the documentation use this look, with different tick lengths/fontsizes for major/minor, when it's not really how it would look by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, now I get your point. The idea was to emphasize the fact that you can control major / minor ticks / labels separately and the different font sizes really suggest this (at least to me).
Since this is a submission to the gallery, is it important to enforce the defaults ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole goal of this example is to show what you can do with axes and ticks. That means changing the default. I don't see how you could do something identical with the defaults. I also don't see how this could confuse the user: the example is well written and clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, was confusing this with the other PR and where it was going.
Also, I thought it looked nice enough to be a default, but I think the time for changing that has passed.