horizontal alignment of a secondary y-axis

Hi Ryan,

Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.

What is "the OO way"?

Your 1st solution gives:
AttributeError: 'module' object has no attribute 'ticks'

I modified your 2nd solution to accommodate my wishes and needs:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
for label in ax2.yaxis.get_ticklabels():
    label.set_horizontalalignment('right')
ax2.tick_params(pad=20)
ax1.plot(list(range(11)))
ax1.set_xlim(0,10)
ax2.set_ylim(0,10)
plt.show()

It seems like an awful hack with that for loop, but it works. I'm not
sure, why the secondary right hand side axis don't have right aligned
labels by default. That would make a lot of sense. It would be great,
if I could set the horizontal alignment without having to use a for
loop. It's just plain ugly. In gnuplot it's as simple as this:
set ytics right

Thanks for your help and providing me with a solution.

Tommy

···

On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@...287...> wrote:

Tommy,

You are probably looking for pyplot.xticks. For example, you might want
something along these lines:

import matplotlib.pyplot as plt
plt.plot([1,3,2])
# We'll do this to get the autogenerated positions
ticks, labels = plt.xticks()
plt.ticks(ticks, horizontalalignment='left')
plt.show()

Or if your using the OO way:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,3,2])
labels = ax.get_xticklabels()
[l.set_horizontalalignment('left') for l in labels]
plt.show()

I think that's the best way. Hope it helps.

Ryan

On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen > <tommy.carstensen@...287...> wrote:

How can I set the horizontal alignment of a secondary y-axis to
'right'? Currently the numbers are glued to the axis. I want the axis
values to be right aligned integers. Thanks.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Tommy,

I’ll try to answer your points in order:

  1. Oops. That should have been “xticks”.

import matplotlib.pyplot as plt

plt.plot([1,3,2])

ticks, labels = plt.xticks()

plt.xticks(ticks, horizontalalignment=‘left’)

plt.show()

  1. Sorry for the ambiguity. “OO” is short for object-oriented. There are two different approaches that people tend to use to make plots (although they can be mixed): 1) the “pyplot” way, which uses the pyplot wrapper functions and 2) the object-oriented way, which modifies the objects directly. This is what you did in your example where you snag the axes objects and operate on them directly. The “OO” way is ultimately more powerful, because the pyplot wrapper functions override some of your control. For example, because you want twin axes, you might not be able to use the pyplot.xticks function (Others, correct me if I’m wrong.), and you lose some fine control. See next example.

  2. I know it seems like the for loop is an “ugly hack”. However, you have to realize that this ultimately gives you a TON of control. Let’s say, for example, that you wanted only one of the labels to be large and red to highlight a certain value. Using a modified version of your example, we get this:

···

On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen <tommy.carstensen@…287…> wrote:

Hi Ryan,

Thanks for your answer. Sorry for not replying sooner. I fell asleep

shortly after sending my question.

What is “the OO way”?

Your 1st solution gives:

AttributeError: ‘module’ object has no attribute ‘ticks’

I modified your 2nd solution to accommodate my wishes and needs:

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

for label in ax2.yaxis.get_ticklabels():

label.set_horizontalalignment('right')

ax2.tick_params(pad=20)

ax1.plot(list(range(11)))

ax1.set_xlim(0,10)

ax2.set_ylim(0,10)

plt.show()

It seems like an awful hack with that for loop, but it works. I’m not

sure, why the secondary right hand side axis don’t have right aligned

labels by default. That would make a lot of sense. It would be great,

if I could set the horizontal alignment without having to use a for

loop. It’s just plain ugly. In gnuplot it’s as simple as this:

set ytics right

Thanks for your help and providing me with a solution.

Tommy

On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@…287…> wrote:

Tommy,

You are probably looking for pyplot.xticks. For example, you might want

something along these lines:

import matplotlib.pyplot as plt

plt.plot([1,3,2])

We’ll do this to get the autogenerated positions

ticks, labels = plt.xticks()

plt.ticks(ticks, horizontalalignment=‘left’)

plt.show()

Or if your using the OO way:

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot([1,3,2])

labels = ax.get_xticklabels()

[l.set_horizontalalignment(‘left’) for l in labels]

plt.show()

I think that’s the best way. Hope it helps.

Ryan

On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen

<tommy.carstensen@…83…287…> wrote:

How can I set the horizontal alignment of a secondary y-axis to

‘right’? Currently the numbers are glued to the axis. I want the axis

values to be right aligned integers. Thanks.


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is

your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more. Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more. Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Whoa, thanks for a great answer Ryan. I can see, why the level of
control MPL gives you is a great sales pitch. It's one of the reasons,
why I switched from gnuplot after using it for many years and making
many cool plots. The MPL learning curve has just been a bit steep,
when you are used to plot whatever you want.

···

On Sat, Feb 14, 2015 at 5:06 PM, Ryan Nelson <rnelsonchem@...287...> wrote:

Tommy,

I'll try to answer your points in order:

1) Oops. That should have been "xticks".
import matplotlib.pyplot as plt
plt.plot([1,3,2])
ticks, labels = plt.xticks()
plt.xticks(ticks, horizontalalignment='left')
plt.show()

2) Sorry for the ambiguity. "OO" is short for object-oriented. There are two
different approaches that people tend to use to make plots (although they
can be mixed): 1) the "pyplot" way, which uses the pyplot wrapper functions
and 2) the object-oriented way, which modifies the objects directly. This is
what you did in your example where you snag the axes objects and operate on
them directly. The "OO" way is ultimately more powerful, because the pyplot
wrapper functions override some of your control. For example, because you
want twin axes, you might not be able to use the pyplot.xticks function
(Others, correct me if I'm wrong.), and you lose some fine control. See next
example.

3) I know it *seems* like the for loop is an "ugly hack". However, you have
to realize that this ultimately gives you a TON of control. Let's say, for
example, that you wanted only one of the labels to be large and red to
highlight a certain value. Using a modified version of your example, we get
this:
______________
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
labels = ax2.yaxis.get_ticklabels()
[l.set_horizontalalignment('right') for l in labels]
labels[2].set_color('red')
labels[2].set_fontsize(20)
ax2.tick_params(pad=20)
ax1.plot(list(range(11)))
ax1.set_xlim(0,10)
ax2.set_ylim(0,10)
plt.show()
____________
I personally think that this level of control is very, very cool and one of
the big selling points for MPL in general.

Okay. If you want to set the alignment all the time, there might be a way to
control this with matplotlibrc or style sheets:
http://matplotlib.org/users/customizing.html
http://matplotlib.org/users/style_sheets.html
However, I'm not the biggest fan of changing matplotlibrc. Mostly because if
others try to reproduce your plots, they also need your rc file as well. I
haven't used style sheets yet, but that might be a fix to this issue (for me
at least).

Hope that helps.

Ryan

On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen > <tommy.carstensen@...287...> wrote:

Hi Ryan,

Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.

What is "the OO way"?

Your 1st solution gives:
AttributeError: 'module' object has no attribute 'ticks'

I modified your 2nd solution to accommodate my wishes and needs:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
for label in ax2.yaxis.get_ticklabels():
    label.set_horizontalalignment('right')
ax2.tick_params(pad=20)
ax1.plot(list(range(11)))
ax1.set_xlim(0,10)
ax2.set_ylim(0,10)
plt.show()

It seems like an awful hack with that for loop, but it works. I'm not
sure, why the secondary right hand side axis don't have right aligned
labels by default. That would make a lot of sense. It would be great,
if I could set the horizontal alignment without having to use a for
loop. It's just plain ugly. In gnuplot it's as simple as this:
set ytics right

Thanks for your help and providing me with a solution.

Tommy

On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@...287...> >> wrote:
> Tommy,
>
> You are probably looking for pyplot.xticks. For example, you might want
> something along these lines:
>
> import matplotlib.pyplot as plt
> plt.plot([1,3,2])
> # We'll do this to get the autogenerated positions
> ticks, labels = plt.xticks()
> plt.ticks(ticks, horizontalalignment='left')
> plt.show()
>
> Or if your using the OO way:
>
> import matplotlib.pyplot as plt
> fig = plt.figure()
> ax = fig.add_subplot(111)
> ax.plot([1,3,2])
> labels = ax.get_xticklabels()
> [l.set_horizontalalignment('left') for l in labels]
> plt.show()
>
> I think that's the best way. Hope it helps.
>
> Ryan
>
>
>
> On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen >> > <tommy.carstensen@...287...> wrote:
>>
>> How can I set the horizontal alignment of a secondary y-axis to
>> 'right'? Currently the numbers are glued to the axis. I want the axis
>> values to be right aligned integers. Thanks.
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Dive into the World of Parallel Programming. The Go Parallel Website,
>> sponsored by Intel and developed in partnership with Slashdot Media, is
>> your
>> hub for all things parallel software development, from weekly thought
>> leadership blogs to news, videos, case studies, tutorials and more.
>> Take a
>> look and join the conversation now. http://goparallel.sourceforge.net/
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

You’re welcome, Tommy. I used gnuplot many years ago, but I’ve been much happier now that I know MPL.

A gnuplot->MPL Rosetta Stone might be a useful blog post for someone. I haven’t used gnuplot in so long that I don’t think I could do this myself.

R

···

On Sat, Feb 14, 2015 at 12:28 PM, Tommy Carstensen <tommy.carstensen@…287…> wrote:

Whoa, thanks for a great answer Ryan. I can see, why the level of

control MPL gives you is a great sales pitch. It’s one of the reasons,

why I switched from gnuplot after using it for many years and making

many cool plots. The MPL learning curve has just been a bit steep,

when you are used to plot whatever you want.

On Sat, Feb 14, 2015 at 5:06 PM, Ryan Nelson <rnelsonchem@…287…> wrote:

Tommy,

I’ll try to answer your points in order:

  1. Oops. That should have been “xticks”.

import matplotlib.pyplot as plt

plt.plot([1,3,2])

ticks, labels = plt.xticks()

plt.xticks(ticks, horizontalalignment=‘left’)

plt.show()

  1. Sorry for the ambiguity. “OO” is short for object-oriented. There are two

different approaches that people tend to use to make plots (although they

can be mixed): 1) the “pyplot” way, which uses the pyplot wrapper functions

and 2) the object-oriented way, which modifies the objects directly. This is

what you did in your example where you snag the axes objects and operate on

them directly. The “OO” way is ultimately more powerful, because the pyplot

wrapper functions override some of your control. For example, because you

want twin axes, you might not be able to use the pyplot.xticks function

(Others, correct me if I’m wrong.), and you lose some fine control. See next

example.

  1. I know it seems like the for loop is an “ugly hack”. However, you have

to realize that this ultimately gives you a TON of control. Let’s say, for

example, that you wanted only one of the labels to be large and red to

highlight a certain value. Using a modified version of your example, we get

this:


import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

labels = ax2.yaxis.get_ticklabels()

[l.set_horizontalalignment(‘right’) for l in labels]

labels[2].set_color(‘red’)

labels[2].set_fontsize(20)

ax2.tick_params(pad=20)

ax1.plot(list(range(11)))

ax1.set_xlim(0,10)

ax2.set_ylim(0,10)

plt.show()


I personally think that this level of control is very, very cool and one of

the big selling points for MPL in general.

Okay. If you want to set the alignment all the time, there might be a way to

control this with matplotlibrc or style sheets:

http://matplotlib.org/users/customizing.html

http://matplotlib.org/users/style_sheets.html

However, I’m not the biggest fan of changing matplotlibrc. Mostly because if

others try to reproduce your plots, they also need your rc file as well. I

haven’t used style sheets yet, but that might be a fix to this issue (for me

at least).

Hope that helps.

Ryan

On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen

<tommy.carstensen@…83…287…> wrote:

Hi Ryan,

Thanks for your answer. Sorry for not replying sooner. I fell asleep

shortly after sending my question.

What is “the OO way”?

Your 1st solution gives:

AttributeError: ‘module’ object has no attribute ‘ticks’

I modified your 2nd solution to accommodate my wishes and needs:

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

for label in ax2.yaxis.get_ticklabels():

label.set_horizontalalignment('right')

ax2.tick_params(pad=20)

ax1.plot(list(range(11)))

ax1.set_xlim(0,10)

ax2.set_ylim(0,10)

plt.show()

It seems like an awful hack with that for loop, but it works. I’m not

sure, why the secondary right hand side axis don’t have right aligned

labels by default. That would make a lot of sense. It would be great,

if I could set the horizontal alignment without having to use a for

loop. It’s just plain ugly. In gnuplot it’s as simple as this:

set ytics right

Thanks for your help and providing me with a solution.

Tommy

On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@…287…>

wrote:

Tommy,

You are probably looking for pyplot.xticks. For example, you might want

something along these lines:

import matplotlib.pyplot as plt

plt.plot([1,3,2])

We’ll do this to get the autogenerated positions

ticks, labels = plt.xticks()

plt.ticks(ticks, horizontalalignment=‘left’)

plt.show()

Or if your using the OO way:

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot([1,3,2])

labels = ax.get_xticklabels()

[l.set_horizontalalignment(‘left’) for l in labels]

plt.show()

I think that’s the best way. Hope it helps.

Ryan

On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen

<tommy.carstensen@…287…> wrote:

How can I set the horizontal alignment of a secondary y-axis to

‘right’? Currently the numbers are glued to the axis. I want the axis

values to be right aligned integers. Thanks.


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is

your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more.

Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is

your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more. Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more. Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Ryan, do you know, if there is any way I can make the padding
dependent on the tick label sizes?
for label in ax2.yaxis.get_ticklabels():
    label.set_horizontalalignment('right')
ax2.tick_params(pad=20)

When the numbers are large, then they are glued to the secondary
y-axis. When they are small, then they are hovering far away from it.

···

On Sat, Feb 14, 2015 at 7:20 PM, Ryan Nelson <rnelsonchem@...287...> wrote:

You're welcome, Tommy. I used gnuplot many years ago, but I've been much
happier now that I know MPL.

A gnuplot->MPL Rosetta Stone might be a useful blog post for someone. I
haven't used gnuplot in so long that I don't think I could do this myself.

R

On Sat, Feb 14, 2015 at 12:28 PM, Tommy Carstensen > <tommy.carstensen@...287...> wrote:

Whoa, thanks for a great answer Ryan. I can see, why the level of
control MPL gives you is a great sales pitch. It's one of the reasons,
why I switched from gnuplot after using it for many years and making
many cool plots. The MPL learning curve has just been a bit steep,
when you are used to plot whatever you want.

On Sat, Feb 14, 2015 at 5:06 PM, Ryan Nelson <rnelsonchem@...287...> >> wrote:
> Tommy,
>
> I'll try to answer your points in order:
>
> 1) Oops. That should have been "xticks".
> import matplotlib.pyplot as plt
> plt.plot([1,3,2])
> ticks, labels = plt.xticks()
> plt.xticks(ticks, horizontalalignment='left')
> plt.show()
>
>
> 2) Sorry for the ambiguity. "OO" is short for object-oriented. There are
> two
> different approaches that people tend to use to make plots (although
> they
> can be mixed): 1) the "pyplot" way, which uses the pyplot wrapper
> functions
> and 2) the object-oriented way, which modifies the objects directly.
> This is
> what you did in your example where you snag the axes objects and operate
> on
> them directly. The "OO" way is ultimately more powerful, because the
> pyplot
> wrapper functions override some of your control. For example, because
> you
> want twin axes, you might not be able to use the pyplot.xticks function
> (Others, correct me if I'm wrong.), and you lose some fine control. See
> next
> example.
>
> 3) I know it *seems* like the for loop is an "ugly hack". However, you
> have
> to realize that this ultimately gives you a TON of control. Let's say,
> for
> example, that you wanted only one of the labels to be large and red to
> highlight a certain value. Using a modified version of your example, we
> get
> this:
> ______________
> import matplotlib.pyplot as plt
> fig = plt.figure()
> ax1 = fig.add_subplot(111)
> ax2 = ax1.twinx()
> labels = ax2.yaxis.get_ticklabels()
> [l.set_horizontalalignment('right') for l in labels]
> labels[2].set_color('red')
> labels[2].set_fontsize(20)
> ax2.tick_params(pad=20)
> ax1.plot(list(range(11)))
> ax1.set_xlim(0,10)
> ax2.set_ylim(0,10)
> plt.show()
> ____________
> I personally think that this level of control is very, very cool and one
> of
> the big selling points for MPL in general.
>
> Okay. If you want to set the alignment all the time, there might be a
> way to
> control this with matplotlibrc or style sheets:
> http://matplotlib.org/users/customizing.html
> http://matplotlib.org/users/style_sheets.html
> However, I'm not the biggest fan of changing matplotlibrc. Mostly
> because if
> others try to reproduce your plots, they also need your rc file as well.
> I
> haven't used style sheets yet, but that might be a fix to this issue
> (for me
> at least).
>
> Hope that helps.
>
> Ryan
>
> On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen >> > <tommy.carstensen@...287...> wrote:
>>
>> Hi Ryan,
>>
>> Thanks for your answer. Sorry for not replying sooner. I fell asleep
>> shortly after sending my question.
>>
>> What is "the OO way"?
>>
>> Your 1st solution gives:
>> AttributeError: 'module' object has no attribute 'ticks'
>>
>> I modified your 2nd solution to accommodate my wishes and needs:
>> import matplotlib.pyplot as plt
>> fig = plt.figure()
>> ax1 = fig.add_subplot(111)
>> ax2 = ax1.twinx()
>> for label in ax2.yaxis.get_ticklabels():
>> label.set_horizontalalignment('right')
>> ax2.tick_params(pad=20)
>> ax1.plot(list(range(11)))
>> ax1.set_xlim(0,10)
>> ax2.set_ylim(0,10)
>> plt.show()
>>
>> It seems like an awful hack with that for loop, but it works. I'm not
>> sure, why the secondary right hand side axis don't have right aligned
>> labels by default. That would make a lot of sense. It would be great,
>> if I could set the horizontal alignment without having to use a for
>> loop. It's just plain ugly. In gnuplot it's as simple as this:
>> set ytics right
>>
>> Thanks for your help and providing me with a solution.
>>
>> Tommy
>>
>> On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@...287...> >> >> wrote:
>> > Tommy,
>> >
>> > You are probably looking for pyplot.xticks. For example, you might
>> > want
>> > something along these lines:
>> >
>> > import matplotlib.pyplot as plt
>> > plt.plot([1,3,2])
>> > # We'll do this to get the autogenerated positions
>> > ticks, labels = plt.xticks()
>> > plt.ticks(ticks, horizontalalignment='left')
>> > plt.show()
>> >
>> > Or if your using the OO way:
>> >
>> > import matplotlib.pyplot as plt
>> > fig = plt.figure()
>> > ax = fig.add_subplot(111)
>> > ax.plot([1,3,2])
>> > labels = ax.get_xticklabels()
>> > [l.set_horizontalalignment('left') for l in labels]
>> > plt.show()
>> >
>> > I think that's the best way. Hope it helps.
>> >
>> > Ryan
>> >
>> >
>> >
>> > On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen >> >> > <tommy.carstensen@...287...> wrote:
>> >>
>> >> How can I set the horizontal alignment of a secondary y-axis to
>> >> 'right'? Currently the numbers are glued to the axis. I want the
>> >> axis
>> >> values to be right aligned integers. Thanks.
>> >>
>> >>
>> >>
>> >>
>> >> ------------------------------------------------------------------------------
>> >> Dive into the World of Parallel Programming. The Go Parallel
>> >> Website,
>> >> sponsored by Intel and developed in partnership with Slashdot Media,
>> >> is
>> >> your
>> >> hub for all things parallel software development, from weekly
>> >> thought
>> >> leadership blogs to news, videos, case studies, tutorials and more.
>> >> Take a
>> >> look and join the conversation now.
>> >> http://goparallel.sourceforge.net/
>> >> _______________________________________________
>> >> Matplotlib-users mailing list
>> >> Matplotlib-users@lists.sourceforge.net
>> >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>> >
>> >
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Dive into the World of Parallel Programming. The Go Parallel Website,
>> sponsored by Intel and developed in partnership with Slashdot Media, is
>> your
>> hub for all things parallel software development, from weekly thought
>> leadership blogs to news, videos, case studies, tutorials and more.
>> Take a
>> look and join the conversation now. http://goparallel.sourceforge.net/
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Tommy,

It would be helpful if you included a more complete example that illustrates the problem. If you are setting the text size yourself, couldn’t you adjust the padding as such “pad=20/txt_size”. Then the padding will be inversely proportional to the size of the text.

I suspect this is related to the text-rendering size issue that I mentioned in the other thread. I think you could do something like the following to get the extent of the label:

http://stackoverflow.com/a/8078114/2662077

Then you could adjust the padding very precisely.

There might be an easier solution for your problem, but without an example, it is hard to say.

Ryan

···

On Sat, Feb 14, 2015 at 2:54 PM, Tommy Carstensen <tommy.carstensen@…287…> wrote:

Ryan, do you know, if there is any way I can make the padding

dependent on the tick label sizes?

for label in ax2.yaxis.get_ticklabels():

label.set_horizontalalignment('right')

ax2.tick_params(pad=20)

When the numbers are large, then they are glued to the secondary

y-axis. When they are small, then they are hovering far away from it.

On Sat, Feb 14, 2015 at 7:20 PM, Ryan Nelson <rnelsonchem@…287…> wrote:

You’re welcome, Tommy. I used gnuplot many years ago, but I’ve been much

happier now that I know MPL.

A gnuplot->MPL Rosetta Stone might be a useful blog post for someone. I

haven’t used gnuplot in so long that I don’t think I could do this myself.

R

On Sat, Feb 14, 2015 at 12:28 PM, Tommy Carstensen

<tommy.carstensen@…83…287…> wrote:

Whoa, thanks for a great answer Ryan. I can see, why the level of

control MPL gives you is a great sales pitch. It’s one of the reasons,

why I switched from gnuplot after using it for many years and making

many cool plots. The MPL learning curve has just been a bit steep,

when you are used to plot whatever you want.

On Sat, Feb 14, 2015 at 5:06 PM, Ryan Nelson <rnelsonchem@…287…>

wrote:

Tommy,

I’ll try to answer your points in order:

  1. Oops. That should have been “xticks”.

import matplotlib.pyplot as plt

plt.plot([1,3,2])

ticks, labels = plt.xticks()

plt.xticks(ticks, horizontalalignment=‘left’)

plt.show()

  1. Sorry for the ambiguity. “OO” is short for object-oriented. There are

two

different approaches that people tend to use to make plots (although

they

can be mixed): 1) the “pyplot” way, which uses the pyplot wrapper

functions

and 2) the object-oriented way, which modifies the objects directly.

This is

what you did in your example where you snag the axes objects and operate

on

them directly. The “OO” way is ultimately more powerful, because the

pyplot

wrapper functions override some of your control. For example, because

you

want twin axes, you might not be able to use the pyplot.xticks function

(Others, correct me if I’m wrong.), and you lose some fine control. See

next

example.

  1. I know it seems like the for loop is an “ugly hack”. However, you

have

to realize that this ultimately gives you a TON of control. Let’s say,

for

example, that you wanted only one of the labels to be large and red to

highlight a certain value. Using a modified version of your example, we

get

this:


import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

labels = ax2.yaxis.get_ticklabels()

[l.set_horizontalalignment(‘right’) for l in labels]

labels[2].set_color(‘red’)

labels[2].set_fontsize(20)

ax2.tick_params(pad=20)

ax1.plot(list(range(11)))

ax1.set_xlim(0,10)

ax2.set_ylim(0,10)

plt.show()


I personally think that this level of control is very, very cool and one

of

the big selling points for MPL in general.

Okay. If you want to set the alignment all the time, there might be a

way to

control this with matplotlibrc or style sheets:

http://matplotlib.org/users/customizing.html

http://matplotlib.org/users/style_sheets.html

However, I’m not the biggest fan of changing matplotlibrc. Mostly

because if

others try to reproduce your plots, they also need your rc file as well.

I

haven’t used style sheets yet, but that might be a fix to this issue

(for me

at least).

Hope that helps.

Ryan

On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen

<tommy.carstensen@…287…> wrote:

Hi Ryan,

Thanks for your answer. Sorry for not replying sooner. I fell asleep

shortly after sending my question.

What is “the OO way”?

Your 1st solution gives:

AttributeError: ‘module’ object has no attribute ‘ticks’

I modified your 2nd solution to accommodate my wishes and needs:

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

for label in ax2.yaxis.get_ticklabels():

label.set_horizontalalignment('right')

ax2.tick_params(pad=20)

ax1.plot(list(range(11)))

ax1.set_xlim(0,10)

ax2.set_ylim(0,10)

plt.show()

It seems like an awful hack with that for loop, but it works. I’m not

sure, why the secondary right hand side axis don’t have right aligned

labels by default. That would make a lot of sense. It would be great,

if I could set the horizontal alignment without having to use a for

loop. It’s just plain ugly. In gnuplot it’s as simple as this:

set ytics right

Thanks for your help and providing me with a solution.

Tommy

On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@…287…>

wrote:

Tommy,

You are probably looking for pyplot.xticks. For example, you might

want

something along these lines:

import matplotlib.pyplot as plt

plt.plot([1,3,2])

We’ll do this to get the autogenerated positions

ticks, labels = plt.xticks()

plt.ticks(ticks, horizontalalignment=‘left’)

plt.show()

Or if your using the OO way:

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot([1,3,2])

labels = ax.get_xticklabels()

[l.set_horizontalalignment(‘left’) for l in labels]

plt.show()

I think that’s the best way. Hope it helps.

Ryan

On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen

<tommy.carstensen@…287…> wrote:

How can I set the horizontal alignment of a secondary y-axis to

‘right’? Currently the numbers are glued to the axis. I want the

axis

values to be right aligned integers. Thanks.


Dive into the World of Parallel Programming. The Go Parallel

Website,

sponsored by Intel and developed in partnership with Slashdot Media,

is

your

hub for all things parallel software development, from weekly

thought

leadership blogs to news, videos, case studies, tutorials and more.

Take a

look and join the conversation now.

http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is

your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more.

Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Dive into the World of Parallel Programming. The Go Parallel Website,

sponsored by Intel and developed in partnership with Slashdot Media, is

your

hub for all things parallel software development, from weekly thought

leadership blogs to news, videos, case studies, tutorials and more. Take a

look and join the conversation now. http://goparallel.sourceforge.net/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Again I did this padding manually by introducing yet another magic
constant. Thank you to you and Erik for your help. After using a lot
of hacks and magic constants here are the final plots:
http://www.tommycarstensen.com/matplotlib1.png
http://www.tommycarstensen.com/matplotlib2.png

Notice 1) the right aligned secondary y-axis labels, 2) the
non-overlapping ticks, 3) the padded secondary y-axis labels. I'm
satisfied with this plot / not willing to spend any more time on it.
Thank you for your help.

···

On Sat, Feb 14, 2015 at 7:54 PM, Tommy Carstensen <tommy.carstensen@...287...> wrote:

Ryan, do you know, if there is any way I can make the padding
dependent on the tick label sizes?
for label in ax2.yaxis.get_ticklabels():
    label.set_horizontalalignment('right')
ax2.tick_params(pad=20)

When the numbers are large, then they are glued to the secondary
y-axis. When they are small, then they are hovering far away from it.

On Sat, Feb 14, 2015 at 7:20 PM, Ryan Nelson <rnelsonchem@...287...> wrote:

You're welcome, Tommy. I used gnuplot many years ago, but I've been much
happier now that I know MPL.

A gnuplot->MPL Rosetta Stone might be a useful blog post for someone. I
haven't used gnuplot in so long that I don't think I could do this myself.

R

On Sat, Feb 14, 2015 at 12:28 PM, Tommy Carstensen >> <tommy.carstensen@...287...> wrote:

Whoa, thanks for a great answer Ryan. I can see, why the level of
control MPL gives you is a great sales pitch. It's one of the reasons,
why I switched from gnuplot after using it for many years and making
many cool plots. The MPL learning curve has just been a bit steep,
when you are used to plot whatever you want.

On Sat, Feb 14, 2015 at 5:06 PM, Ryan Nelson <rnelsonchem@...287...> >>> wrote:
> Tommy,
>
> I'll try to answer your points in order:
>
> 1) Oops. That should have been "xticks".
> import matplotlib.pyplot as plt
> plt.plot([1,3,2])
> ticks, labels = plt.xticks()
> plt.xticks(ticks, horizontalalignment='left')
> plt.show()
>
>
> 2) Sorry for the ambiguity. "OO" is short for object-oriented. There are
> two
> different approaches that people tend to use to make plots (although
> they
> can be mixed): 1) the "pyplot" way, which uses the pyplot wrapper
> functions
> and 2) the object-oriented way, which modifies the objects directly.
> This is
> what you did in your example where you snag the axes objects and operate
> on
> them directly. The "OO" way is ultimately more powerful, because the
> pyplot
> wrapper functions override some of your control. For example, because
> you
> want twin axes, you might not be able to use the pyplot.xticks function
> (Others, correct me if I'm wrong.), and you lose some fine control. See
> next
> example.
>
> 3) I know it *seems* like the for loop is an "ugly hack". However, you
> have
> to realize that this ultimately gives you a TON of control. Let's say,
> for
> example, that you wanted only one of the labels to be large and red to
> highlight a certain value. Using a modified version of your example, we
> get
> this:
> ______________
> import matplotlib.pyplot as plt
> fig = plt.figure()
> ax1 = fig.add_subplot(111)
> ax2 = ax1.twinx()
> labels = ax2.yaxis.get_ticklabels()
> [l.set_horizontalalignment('right') for l in labels]
> labels[2].set_color('red')
> labels[2].set_fontsize(20)
> ax2.tick_params(pad=20)
> ax1.plot(list(range(11)))
> ax1.set_xlim(0,10)
> ax2.set_ylim(0,10)
> plt.show()
> ____________
> I personally think that this level of control is very, very cool and one
> of
> the big selling points for MPL in general.
>
> Okay. If you want to set the alignment all the time, there might be a
> way to
> control this with matplotlibrc or style sheets:
> http://matplotlib.org/users/customizing.html
> http://matplotlib.org/users/style_sheets.html
> However, I'm not the biggest fan of changing matplotlibrc. Mostly
> because if
> others try to reproduce your plots, they also need your rc file as well.
> I
> haven't used style sheets yet, but that might be a fix to this issue
> (for me
> at least).
>
> Hope that helps.
>
> Ryan
>
> On Sat, Feb 14, 2015 at 10:30 AM, Tommy Carstensen >>> > <tommy.carstensen@...287...> wrote:
>>
>> Hi Ryan,
>>
>> Thanks for your answer. Sorry for not replying sooner. I fell asleep
>> shortly after sending my question.
>>
>> What is "the OO way"?
>>
>> Your 1st solution gives:
>> AttributeError: 'module' object has no attribute 'ticks'
>>
>> I modified your 2nd solution to accommodate my wishes and needs:
>> import matplotlib.pyplot as plt
>> fig = plt.figure()
>> ax1 = fig.add_subplot(111)
>> ax2 = ax1.twinx()
>> for label in ax2.yaxis.get_ticklabels():
>> label.set_horizontalalignment('right')
>> ax2.tick_params(pad=20)
>> ax1.plot(list(range(11)))
>> ax1.set_xlim(0,10)
>> ax2.set_ylim(0,10)
>> plt.show()
>>
>> It seems like an awful hack with that for loop, but it works. I'm not
>> sure, why the secondary right hand side axis don't have right aligned
>> labels by default. That would make a lot of sense. It would be great,
>> if I could set the horizontal alignment without having to use a for
>> loop. It's just plain ugly. In gnuplot it's as simple as this:
>> set ytics right
>>
>> Thanks for your help and providing me with a solution.
>>
>> Tommy
>>
>> On Sat, Feb 14, 2015 at 1:31 AM, Ryan Nelson <rnelsonchem@...287...> >>> >> wrote:
>> > Tommy,
>> >
>> > You are probably looking for pyplot.xticks. For example, you might
>> > want
>> > something along these lines:
>> >
>> > import matplotlib.pyplot as plt
>> > plt.plot([1,3,2])
>> > # We'll do this to get the autogenerated positions
>> > ticks, labels = plt.xticks()
>> > plt.ticks(ticks, horizontalalignment='left')
>> > plt.show()
>> >
>> > Or if your using the OO way:
>> >
>> > import matplotlib.pyplot as plt
>> > fig = plt.figure()
>> > ax = fig.add_subplot(111)
>> > ax.plot([1,3,2])
>> > labels = ax.get_xticklabels()
>> > [l.set_horizontalalignment('left') for l in labels]
>> > plt.show()
>> >
>> > I think that's the best way. Hope it helps.
>> >
>> > Ryan
>> >
>> >
>> >
>> > On Fri, Feb 13, 2015 at 7:29 PM, Tommy Carstensen >>> >> > <tommy.carstensen@...287...> wrote:
>> >>
>> >> How can I set the horizontal alignment of a secondary y-axis to
>> >> 'right'? Currently the numbers are glued to the axis. I want the
>> >> axis
>> >> values to be right aligned integers. Thanks.
>> >>
>> >>
>> >>
>> >>
>> >> ------------------------------------------------------------------------------
>> >> Dive into the World of Parallel Programming. The Go Parallel
>> >> Website,
>> >> sponsored by Intel and developed in partnership with Slashdot Media,
>> >> is
>> >> your
>> >> hub for all things parallel software development, from weekly
>> >> thought
>> >> leadership blogs to news, videos, case studies, tutorials and more.
>> >> Take a
>> >> look and join the conversation now.
>> >> http://goparallel.sourceforge.net/
>> >> _______________________________________________
>> >> Matplotlib-users mailing list
>> >> Matplotlib-users@lists.sourceforge.net
>> >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>> >
>> >
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Dive into the World of Parallel Programming. The Go Parallel Website,
>> sponsored by Intel and developed in partnership with Slashdot Media, is
>> your
>> hub for all things parallel software development, from weekly thought
>> leadership blogs to news, videos, case studies, tutorials and more.
>> Take a
>> look and join the conversation now. http://goparallel.sourceforge.net/
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users