X Tutup
Skip to content
Merged
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.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions examples/ticks_and_spines/tick-formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def setup(ax):
ax.patch.set_alpha(0.0)


plt.figure(figsize=(8, 6))
fig = plt.figure(figsize=(8, 6))
n = 7

# Null formatter
ax = plt.subplot(n, 1, 1)
ax = fig.add_subplot(n, 1, 1)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
Expand All @@ -38,7 +38,7 @@ def setup(ax):
ax.text(0.0, 0.1, "NullFormatter()", fontsize=16, transform=ax.transAxes)

# Fixed formatter
ax = plt.subplot(n, 1, 2)
ax = fig.add_subplot(n, 1, 2)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
Expand All @@ -51,22 +51,23 @@ def setup(ax):
fontsize=15, transform=ax.transAxes)


# Func formatter
# FuncFormatter can be used as a decorator
@ticker.FuncFormatter
def major_formatter(x, pos):
return "[%.2f]" % x


ax = plt.subplot(n, 1, 3)
ax = fig.add_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.xaxis.set_major_formatter(major_formatter)
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.

I missed the PR, but I think if you really want to suggest using decorator syntax, you can do even better with

@ax.xaxis.set_major_formatter
@ticker.FuncFormatter
def _(x, pos):
    return ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's pretty cool, but I think it's a little too clever for the examples/gallery.

My main goal to show how to explicitly act on the Axes/Axis objects and avoid nested function calls.

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.

That's fair.
As a side note this file should be renamed tick_formatters.py consistently with other examples which have importable names.

ax.text(0.0, 0.1, 'FuncFormatter(lambda x, pos: "[%.2f]" % x)',
fontsize=15, transform=ax.transAxes)


# FormatStr formatter
ax = plt.subplot(n, 1, 4)
ax = fig.add_subplot(n, 1, 4)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
Expand All @@ -75,15 +76,15 @@ def major_formatter(x, pos):
fontsize=15, transform=ax.transAxes)

# Scalar formatter
ax = plt.subplot(n, 1, 5)
ax = fig.add_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.1, "ScalarFormatter()", fontsize=15, transform=ax.transAxes)

# StrMethod formatter
ax = plt.subplot(n, 1, 6)
ax = fig.add_subplot(n, 1, 6)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
Expand All @@ -92,7 +93,7 @@ def major_formatter(x, pos):
fontsize=15, transform=ax.transAxes)

# Percent formatter
ax = plt.subplot(n, 1, 7)
ax = fig.add_subplot(n, 1, 7)
setup(ax)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
Expand All @@ -102,6 +103,6 @@ def major_formatter(x, pos):

# Push the top of the top axes outside the figure because we only show the
# bottom spine.
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)
fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)

plt.show()
X Tutup