drawing figure outside axis

Say I have drawn a plot and now I want to make an

    > annotation on the top of the plot outside the axis. In my
    > case I want to put a filled triangle up there in a certain
    > location. I am using a polygon - patch to make the figure.

    > How does one do this? I have tried turning the clipping off
    > but to no avail - I can put my triangle anywhere within the
    > axis but if it is outside it disappears. In the manual
    > there is an example for placing text outside the axis but
    > not patches.

The problem you are encountering is that the Axes will automatically
set the clipbox when you call add_patch, so you need to make the call
to turn off clipping *after* adding it to the axes

    from pylab import figure, show
    from matplotlib.patches import RegularPolygon

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

    # above the yaxis and centered on xaxis; Axes coords
    tri = RegularPolygon((0.5, 1.05), 3, radius=0.2, transform=ax.transAxes)

    # adding the patch to the axes automatically sets the clipbox
    ax.add_patch(tri)

    # so you need to turn it off after adding it
    tri.set_clip_on(False)

    show()

You can also add patches and lines directly to the Figure instance, as
follows, but these are drawn before the Axes and so are behind them

    from pylab import figure, show
    from matplotlib.patches import RegularPolygon

    fig = figure()
    ax = fig.add_axes([0.1,0.1, 0.8, 0.6])

    # Figure coords
    tri = RegularPolygon((0.5, 0.8), 3, radius=0.2, transform=fig.transFigure)

    fig.patches.append(tri)

    show()

If there is a need to customize this further, let me know. Eg, we
could move the drawing call for the lines and patches below the Axes
draw so they come out on top.

JDH

Thanks John this works just fine - I consistently set the clipping off - BEFORE adding the patch.

--Jim

···

On Jun 29, 2005, at 1:13 PM, John Hunter wrote:

    > Say I have drawn a plot and now I want to make an
    > annotation on the top of the plot outside the axis. In my
    > case I want to put a filled triangle up there in a certain
    > location. I am using a polygon - patch to make the figure.

    > How does one do this? I have tried turning the clipping off
    > but to no avail - I can put my triangle anywhere within the
    > axis but if it is outside it disappears. In the manual
    > there is an example for placing text outside the axis but
    > not patches.

The problem you are encountering is that the Axes will automatically
set the clipbox when you call add_patch, so you need to make the call
to turn off clipping *after* adding it to the axes

    from pylab import figure, show
    from matplotlib.patches import RegularPolygon

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

    # above the yaxis and centered on xaxis; Axes coords
    tri = RegularPolygon((0.5, 1.05), 3, radius=0.2, transform=ax.transAxes)

    # adding the patch to the axes automatically sets the clipbox
    ax.add_patch(tri)

    # so you need to turn it off after adding it
    tri.set_clip_on(False)

    show()

You can also add patches and lines directly to the Figure instance, as
follows, but these are drawn before the Axes and so are behind them

    from pylab import figure, show
    from matplotlib.patches import RegularPolygon

    fig = figure()
    ax = fig.add_axes([0.1,0.1, 0.8, 0.6])

    # Figure coords
    tri = RegularPolygon((0.5, 0.8), 3, radius=0.2, transform=fig.transFigure)

    fig.patches.append(tri)

    show()

If there is a need to customize this further, let me know. Eg, we
could move the drawing call for the lines and patches below the Axes
draw so they come out on top.

JDH