Customizing Tick and Label Style

Once you’ve got the correct strings printing along the axes, with as many (or few) ticks as you want, and at the right spacing, there are a lot of options for the way you can adjust the style of the rendering.

Let’s start with a simple graph.

%pylab inline

x = y = np.linspace(0, 10)

fig, ax = plt.subplots()
ax.plot(x, y)
Populating the interactive namespace from numpy and matplotlib





[<matplotlib.lines.Line2D at 0xa36c0b8>]

png

Changing Appearance

The tick_params() method for an Axis object gives you access to a whole bunch of options for formatting your ticks and their labels, including:

If the tick faces in or out

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               direction='in')

png

How much the label is rotated

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               rotation=90)

png

How thick a tick is

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               width=10)

png

The space between label and tick

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               pad=20)

png

The color of the ticks

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               color='orange',
               width=5)

png

Hiding Them Altogether

We can also control whether or not the tick shows up

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               bottom=False)

png

Or the label

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='x',
               labelbottom=False)

png

Or all of it

fig, ax = plt.subplots()
ax.plot(x, y)
ax.tick_params(axis='both',
               labelbottom=False,
               bottom=False,
               labelleft=False,
               left=False)

png