Version 3.0.2
matplotlib

Table of Contents

Related Topics

Dollar Ticks

Use a FormatStrFormatter to prepend dollar signs on y axis labels.

============
Dollar Ticks
============

Use a `~.ticker.FormatStrFormatter` to prepend dollar signs on y axis labels.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# Fixing random state for reproducibility
np.random.seed(19680801)

fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))

formatter = ticker.FormatStrFormatter('$%1.2f')
ax.yaxis.set_major_formatter(formatter)

for tick in ax.yaxis.get_major_ticks():
    tick.label1.set_visible(False)
    tick.label2.set_visible(True)
    tick.label2.set_color('green')

plt.show()
Traceback (most recent call last):
  File "/build/matplotlib-xz4j8t/matplotlib-3.1.2/examples/pyplots/dollar_ticks.py", line 1
    ============
    ^
SyntaxError: invalid syntax

References

The use of the following functions, methods, classes and modules is shown in this example:

import matplotlib
matplotlib.ticker
matplotlib.ticker.FormatStrFormatter
matplotlib.axis.Axis.set_major_formatter
matplotlib.axis.Axis.get_major_ticks
matplotlib.axis.Tick

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery