Multiple lines of x tick labels in matplotlib

Viewed 35546

I'm trying to make a plot similar to this excel example:

example

I would like to know if there is anyways to have a second layer on the x tick labels (e.g. "5 Year Statistical Summary"). I know I can make multi-line tick labels using \n but I want to be able to shift the two levels independently.

5 Answers

I am also unable to comment due to reputation but have a minor fix for behzad's answer.

The tick_params() 'bottom' and 'top' keywords take booleans, not strings (at least for py3.6). For example, using bottom = 'off' for me still produces ticks whereas using bottom = False removes ticks.

so replace

ax.tick_params( axis='x', which='major', bottom='off', top='off' )

with

ax.tick_params( axis='x', which='major', bottom=False, top=False )

and it works!

Here is a Matplotlib reference/example for multiple x-tick lines: Multiline

it uses the same approach as omegamanda

ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.],
               labels=["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009",
                       "May\n2009"])
Related