move xaxis label on the right

As in the title: usually axis label on the x axis is plottet in the
middle of the axis. How to move it on the right? (and on the top for y
axis)

You cannot coerce the xlabel to the top because it's y position is
determined at drawtime to avoid overlapping the tick labels. But you
can place an arbitrary piece of text up there

text(1.0, 1.02, 'testing', horizontalalignment='right',
verticalalignment='bottom', fontsize=14)

See also

  http://matplotlib.sourceforge.net/users/index_text.html

and

  http://matplotlib.sourceforge.net/examples/pylab_examples/alignment_test.html

JDH

···

On Tue, Sep 28, 2010 at 10:04 AM, Ruggero <giurrero@...287...> wrote:

As in the title: usually axis label on the x axis is plottet in the
middle of the axis. How to move it on the right? (and on the top for y
axis)

Oops, forgot to set the transform. That should read

text(1.0, 1.02, 'testing', horizontalalignment='right',
verticalalignment='bottom',
  fontsize=14, transform=ax.transAxes)

For more on transforms and the various coordinate systems, see

http://matplotlib.sourceforge.net/users/transforms_tutorial.html

JDH

···

On Tue, Sep 28, 2010 at 11:29 AM, John Hunter <jdh2358@...287...> wrote:

On Tue, Sep 28, 2010 at 10:04 AM, Ruggero <giurrero@...287...> wrote:

As in the title: usually axis label on the x axis is plottet in the
middle of the axis. How to move it on the right? (and on the top for y
axis)

You cannot coerce the xlabel to the top because it's y position is
determined at drawtime to avoid overlapping the tick labels. But you
can place an arbitrary piece of text up there

text(1.0, 1.02, 'testing', horizontalalignment='right',
verticalalignment='bottom', fontsize=14)

2010/9/28 John Hunter <jdh2358@...287...>:

···

On Tue, Sep 28, 2010 at 10:04 AM, Ruggero <giurrero@...287...> wrote:

As in the title: usually axis label on the x axis is plottet in the
middle of the axis. How to move it on the right? (and on the top for y
axis)

You cannot coerce the xlabel to the top because it's y position is
determined at drawtime to avoid overlapping the tick labels. But you
can place an arbitrary piece of text up there

text(1.0, 1.02, 'testing', horizontalalignment='right',
verticalalignment='bottom', fontsize=14)

See also

http://matplotlib.sourceforge.net/users/index_text.html

and

http://matplotlib.sourceforge.net/examples/pylab_examples/alignment_test.html

JDH

really there is no way to place the xlabel on the right as here:
http://www.cernlove.org/blog/wp-content/uploads/2009/10/root_plot1d.png ?

I don't want to edit the y position of x label, I want to modify the x
position of x label and the y position of y label

Yes, there is…

When calling .set_xlabel(), you can specify the x coordinate of the label (in units of percentage of the axis width) without naming the y coordinate (that will be set automatically). You might have to fiddle with the ‘ha’ (horizontalalignment) keyword (choices are ‘center’, ‘left’, ‘right’) in order to align your text relative to the coordinate you choose.

ex:
ax.set_xlabel(‘Time (s)’, x=0.95, ha=‘right’)

You can do the same thing with the ‘y’ keyword in .set_ylabel(). Instead of ‘ha’, you use ‘va’ (verticalalignment) in that case (choices: ‘top’, ‘bottom’, ‘center’, ‘baseline’).

Maybe we should add an example into the documentation for set_xlabel() and set_ylabel()? I can see this as a reasonable enough feature for some to want to seek out.

Ben Root

P.S. - I just noticed that the axis label ‘remembers’ the keyword properties that were used in previous calls to set_?label() on that axis. In other words, if at one point I specify that the ylabel should be at y=0.25 and then in a subsequent call to set_ylabel(), I don’t specify anything at all, the previous value is used rather than the default.

Don’t know if that is a feature or a bug, but this kind of thing would be more obvious as matplotlib becomes more interactive. At the least, it probably should be documented in some way.

···

On Tue, Sep 28, 2010 at 2:10 PM, Ruggero <giurrero@…287…> wrote:

2010/9/28 John Hunter <jdh2358@…287…>:

On Tue, Sep 28, 2010 at 10:04 AM, Ruggero <giurrero@…287…> wrote:

As in the title: usually axis label on the x axis is plottet in the

middle of the axis. How to move it on the right? (and on the top for y

axis)

You cannot coerce the xlabel to the top because it’s y position is

determined at drawtime to avoid overlapping the tick labels. But you

can place an arbitrary piece of text up there

text(1.0, 1.02, ‘testing’, horizontalalignment=‘right’,

verticalalignment=‘bottom’, fontsize=14)

See also

http://matplotlib.sourceforge.net/users/index_text.html

and

http://matplotlib.sourceforge.net/examples/pylab_examples/alignment_test.html

JDH

really there is no way to place the xlabel on the right as here:

http://www.cernlove.org/blog/wp-content/uploads/2009/10/root_plot1d.png ?

I don’t want to edit the y position of x label, I want to modify the x

position of x label and the y position of y label

Sorry, I misread the meaning of "and on the top for y axis". As Ben
noted, if all you want to do is move the x-alignment, you can pass
horizontalalignment to the xlabel command.

···

On Tue, Sep 28, 2010 at 2:10 PM, Ruggero <giurrero@...287...> wrote:

really there is no way to place the xlabel on the right as here:
http://www.cernlove.org/blog/wp-content/uploads/2009/10/root_plot1d.png ?

Just to make it clear for anyone coming across this thread, horizontalalignment alone won’t do it. ‘ha’ only specifies the alignment relative to the text coordinate point, which is 0.5 by default for axis labels. It is by specifying the x (for xlabel) or the y (for ylabel) that will actually move the axis label in any usable fashion.

Ben Root

···

On Tue, Sep 28, 2010 at 3:05 PM, John Hunter <jdh2358@…287…> wrote:

On Tue, Sep 28, 2010 at 2:10 PM, Ruggero <giurrero@…287…> wrote:

really there is no way to place the xlabel on the right as here:

http://www.cernlove.org/blog/wp-content/uploads/2009/10/root_plot1d.png ?

Sorry, I misread the meaning of “and on the top for y axis”. As Ben

noted, if all you want to do is move the x-alignment, you can pass

horizontalalignment to the xlabel command.