Take the 2-minute tour ×

I'm currently using matplotlib to plot a measurement against 2 or 3 other measurements (sometimes categorical) on the x-axis. Currently, I am grouping the data on the x-axis into tuples and sorting them before plotting... the result looks something like the left image below. What I would like to do is to plot the data with multiple x-axes as you see in the right image. The grouping of the "treatment" x-axis labels would be icing on the cake.

alt text

share|improve this question
add comment

2 Answers

up vote 14 down vote accepted

First off, cool question! It's definitely possible with matplotlib >= 1.0.0. (The new spines functionality allows it)

It requires a fair bit of voodoo, though... My example is far from perfect, but hopefully it makes some sense:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

def main():
    nx = 10
    x = np.linspace(0, 2*np.pi, 10)
    y = 2 * np.sin(x)

    groups = [('GroupA', (x[0], x[nx//3])),
              ('GroupB', (x[-2*nx//3], x[2*nx//3])),
              ('GroupC', (x[-nx//3], x[-1]))]

    fig = plt.figure()
    ax = fig.add_subplot(111)

    # Give ourselves a bit more room at the bottom

    ax.plot(x,y, 'k^')

    # Drop the bottom spine by 40 pts
    ax.spines['bottom'].set_position(('outward', 40))

    # Make a second bottom spine in the position of the original bottom spine

    # Annotate the groups
    for name, xspan in groups:
        annotate_group(name, xspan)

    plt.title('Experimental Data')


def annotate_group(name, xspan, ax=None):
    """Annotates a span of the x-axis"""
    def annotate(ax, name, left, right, y, pad):
        arrow = ax.annotate(name,
                xy=(left, y), xycoords='data',
                xytext=(right, y-pad), textcoords='data',
                annotation_clip=False, verticalalignment='top',
                horizontalalignment='center', linespacing=2.0,
                arrowprops=dict(arrowstyle='-', shrinkA=0, shrinkB=0,
        return arrow
    if ax is None:
        ax = plt.gca()
    ymin = ax.get_ylim()[0]
    ypad = 0.01 * np.ptp(ax.get_ylim())
    xcenter = np.mean(xspan)
    left_arrow = annotate(ax, name, xspan[0], xcenter, ymin, ypad)
    right_arrow = annotate(ax, name, xspan[1], xcenter, ymin, ypad)
    return left_arrow, right_arrow

def make_second_bottom_spine(ax=None, label=None, offset=0, labeloffset=20):
    """Makes a second bottom spine"""
    if ax is None:
        ax = plt.gca()
    second_bottom = mpl.spines.Spine(ax, 'bottom', ax.spines['bottom']._path)
    second_bottom.set_position(('outward', offset))
    ax.spines['second_bottom'] = second_bottom

    if label is not None:
        # Make a new xlabel
                xy=(0.5, 0), xycoords='axes fraction', 
                xytext=(0, -labeloffset), textcoords='offset points', 
                verticalalignment='top', horizontalalignment='center')

if __name__ == '__main__':

Two bottom spines in a matplotlib plot

share|improve this answer
I'm unfamiliar with this voodoo - care to show how to generalize this to more categorical axes? I thought creating a third bottom spine with some offset would make it visible, but that's not working for me - it's still stacked right on top of the second. (I can open a new question if that's perferable) –  Thomas Feb 9 '11 at 18:28
nm I've got it now - if you like though I would still love to see your (cleaner) implementation of it. –  Thomas Feb 9 '11 at 18:50
add comment

Joe's example is good. I'll throw mine in too. I was working on it a few hours ago, but then had to run off to a meeting. It steals from here.

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

## the following two functions override the default behavior or twiny()
def make_patch_spines_invisible(ax):
    for sp in ax.spines.itervalues():

def make_spine_invisible(ax, direction):
    if direction in ["right", "left"]:
    elif direction in ["top", "bottom"]:
        raise ValueError("Unknown Direction : %s" % (direction,))


data = (('A',0.01),('A',0.02),('B',0.10),('B',0.20)) # fake data

fig = plt.figure(1)
sb = fig.add_subplot(111)

sb.plot([i[1] for i in data],"*",markersize=10)

plt.subplots_adjust(bottom=0.17) # make room on bottom

par2 = sb.twiny() # create a second axes
par2.spines["bottom"].set_position(("axes", -.1)) # move it down

## override the default behavior for a twiny axis
make_spine_invisible(par2, "bottom")

par2.plot([i[1] for i in data],"*",markersize=10) #redraw to put twiny on same scale
par2.xaxis.set_ticklabels([i[0] for i in data])



alt text

share|improve this answer
add comment

Your Answer


