You are reading an old version of the documentation (v1.5.0). For the latest version see https://matplotlib.org/stable/

We're updating the default styles for Matplotlib 2.0

Learn what to expect in the new updates

matplotlib

Previous topic

ticks_and_spines example code: spines_demo_dropped.py

Next topic

ticks_and_spines example code: ticklabels_demo_rotation.py

This Page

ticks_and_spines example code: tick_labels_from_values.pyΒΆ

(Source code, png, hires.png, pdf)

../../_images/tick_labels_from_values.png
"""

Basic demo showing how to set tick labels to values of a series.

Using ax.set_xticks causes the tick labels to be set on the currently
chosen ticks. However, you may want to allow matplotlib to dynamically
choose the number of ticks and their spacing.

In this case it may be better to determine the tick label from the
value at the tick. The following example shows how to do this.

NB: The MaxNLocator is used here to ensure that the tick values
take integer values.

"""

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig = plt.figure()
ax = fig.add_subplot(111)
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')


def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''
ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.plot(xs, ys)
plt.show()

Keywords: python, matplotlib, pylab, example, codex (see Search examples)