[n00b] removing y axis and keeping x

I'm a total beginner to matplotlib so please forgive me if this is
standard stuff. I've read through the user guide, tutorial, searched
all over the place but couldn't figure it out on my own.

Normally, matplotlib puts the graph in a box, left y axis, bottom x
axis, right y axis, top x axis. What I'd like to do is remove both y
axis and the top x axis and only keep the bottom x axis. How do I do
that?

I also would like to save the plot in PNG format which works great.
However, the PNG image is not only the graph but also some space
around it in all 4 directions. How do I force matplotlib to write only
the box that contains the graph to the file and no padding around it?

Cheers,
Daniel

Daniel Fetchinson wrote:

I'm a total beginner to matplotlib so please forgive me if this is
standard stuff. I've read through the user guide, tutorial, searched
all over the place but couldn't figure it out on my own.

Normally, matplotlib puts the graph in a box, left y axis, bottom x
axis, right y axis, top x axis. What I'd like to do is remove both y
axis and the top x axis and only keep the bottom x axis. How do I do
that?

This is an often-requested feature, and several times I have been on the verge of adding it, but I still haven't done it. I think that JDH has posted workaround code for manually making the desired axes, but I don't recall where. It really should be a simple option in mpl, though.

I also would like to save the plot in PNG format which works great.
However, the PNG image is not only the graph but also some space
around it in all 4 directions. How do I force matplotlib to write only
the box that contains the graph to the file and no padding around it?

This also comes up quite frequently. You can adjust the position of the axes in the figure by making the axes manually. The pyplot (or pylab) command for making an axes includes this in its docstring:

     axes(rect, axisbg='w') where rect=[left, bottom, width, height] in
     normalized (0,1) units. axisbg is the background color for the
     axis, default white

So if you don't need any labels, or anything outside the axes box, you can use:

ax = axes([0,0,1,1])

or change the rect argument as needed.

Eric

···

Cheers,
Daniel

> I'm a total beginner to matplotlib so please forgive me if this is
> standard stuff. I've read through the user guide, tutorial, searched
> all over the place but couldn't figure it out on my own.
>
> Normally, matplotlib puts the graph in a box, left y axis, bottom x
> axis, right y axis, top x axis. What I'd like to do is remove both y
> axis and the top x axis and only keep the bottom x axis. How do I do
> that?

This is an often-requested feature, and several times I have been on the
verge of adding it, but I still haven't done it. I think that JDH has
posted workaround code for manually making the desired axes, but I don't
recall where. It really should be a simple option in mpl, though.

Yes, this would be really useful. For the moment I'll just turn off
all axis, I managed to do that. By the way is it possible to just turn
off the axis and not the tick labels?

> I also would like to save the plot in PNG format which works great.
> However, the PNG image is not only the graph but also some space
> around it in all 4 directions. How do I force matplotlib to write only
> the box that contains the graph to the file and no padding around it?

This also comes up quite frequently. You can adjust the position of the
axes in the figure by making the axes manually. The pyplot (or pylab)
command for making an axes includes this in its docstring:

     axes(rect, axisbg='w') where rect=[left, bottom, width, height] in
     normalized (0,1) units. axisbg is the background color for the
     axis, default white

So if you don't need any labels, or anything outside the axes box, you
can use:

ax = axes([0,0,1,1])

or change the rect argument as needed.

Maybe I'm misunderstanding something but this doesn't really work as
intended. I have this:

    figure = pylab.figure( )
    figure.add_axes( [0,0,1,1] ) # this I added following your advice
    graph = figure.add_subplot(111)
    graph.plot_date( xdata, ydata, '-' )
    graph.xaxis.set_major_locator( YearLocator( ) )
    graph.xaxis.set_major_formatter( DateFormatter( "'%y" ) )
    graph.xaxis.set_minor_locator( MonthLocator( ) )
    graph.set_yticklabels( ( ) )
    graph.set_yticks( ( ) )
    graph.autoscale_view( )
    figure.savefig( 'hello.png' )

and the result is that I have two sets of axis, one from the original
figure that I had already and an additional that is positioned really
at the boundary of the image so where I'd like to have it, but the
graph with its original axis is still there in the middle.

What am I doing wrong?

Cheers,
Daniel

Daniel Fetchinson wrote:
[...]

Yes, this would be really useful. For the moment I'll just turn off
all axis, I managed to do that. By the way is it possible to just turn
off the axis and not the tick labels?

In your example below you could try
graph.set_frame_on(False)
That will leave the ticks and the tick labels but remove the box and
the axes background, leaving the figure background (which you can set if needed). If you also want to knock out the ticks, you can do this:

tl = graph.xaxis.get_ticklines()
for t in tl:
     t.set_visible(False)

and similarly for yaxis.

[...]

Maybe I'm misunderstanding something but this doesn't really work as
intended. I have this:

    figure = pylab.figure( )
    figure.add_axes( [0,0,1,1] ) # this I added following your advice
    graph = figure.add_subplot(111)

Replace the last two lines above with the single line:
       graph = figure.add_axes( [0,0,1,1] )
and the rest should work.

Eric

Great, thanks very much!

Cheers,
Daniel

···

On 4/19/08, Eric Firing <efiring@...202...> wrote:

Daniel Fetchinson wrote:
[...]
> Yes, this would be really useful. For the moment I'll just turn off
> all axis, I managed to do that. By the way is it possible to just turn
> off the axis and not the tick labels?

In your example below you could try
graph.set_frame_on(False)
That will leave the ticks and the tick labels but remove the box and
the axes background, leaving the figure background (which you can set if
needed). If you also want to knock out the ticks, you can do this:

tl = graph.xaxis.get_ticklines()
for t in tl:
     t.set_visible(False)

and similarly for yaxis.

[...]
> Maybe I'm misunderstanding something but this doesn't really work as
> intended. I have this:
>
> figure = pylab.figure( )
> figure.add_axes( [0,0,1,1] ) # this I added following your advice
> graph = figure.add_subplot(111)

Replace the last two lines above with the single line:
       graph = figure.add_axes( [0,0,1,1] )
and the rest should work.

Eric