Discussion:
[Matplotlib-users] horizontal alignment of a secondary y-axis
Tommy Carstensen
2015-02-14 00:29:09 UTC
Permalink
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.
Tommy Carstensen
2015-02-14 15:30:32 UTC
Permalink
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
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 17:06:44 UTC
Permalink
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 <
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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.
------------------------------------------------------------------------------
Post by Tommy Carstensen
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
Post by Tommy Carstensen
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 17:28:08 UTC
Permalink
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.
Tommy,
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
______________
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
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
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 19:20:03 UTC
Permalink
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 <
Post by Tommy Carstensen
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.
Tommy,
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
______________
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
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
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might
want
Post by Tommy Carstensen
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()
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
Post by Tommy Carstensen
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.
------------------------------------------------------------------------------
Post by Tommy Carstensen
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Tommy Carstensen
Post by Tommy Carstensen
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/
Post by Tommy Carstensen
Post by Tommy Carstensen
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Tommy Carstensen
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
Post by Tommy Carstensen
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 19:54:37 UTC
Permalink
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.
Post by Ryan Nelson
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
Post by Tommy Carstensen
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.
Tommy,
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
______________
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
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
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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
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
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Ryan Nelson
2015-02-14 20:17:28 UTC
Permalink
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 <
Post by Tommy Carstensen
Ryan, do you know, if there is any way I can make the padding
dependent on the tick label sizes?
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.
Post by Ryan Nelson
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.
Post by Ryan Nelson
R
On Sat, Feb 14, 2015 at 12:28 PM, Tommy Carstensen
Post by Tommy Carstensen
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.
Tommy,
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
Post by Ryan Nelson
Post by Tommy Carstensen
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
Post by Ryan Nelson
Post by Tommy Carstensen
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
Post by Ryan Nelson
Post by Tommy Carstensen
(Others, correct me if I'm wrong.), and you lose some fine control.
See
Post by Ryan Nelson
Post by Tommy Carstensen
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
Post by Ryan Nelson
Post by Tommy Carstensen
get
______________
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
Post by Ryan Nelson
Post by Tommy Carstensen
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
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.
Post by Ryan Nelson
Post by Tommy Carstensen
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
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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.
------------------------------------------------------------------------------
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot
Media,
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
Post by Tommy Carstensen
is
your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and
more.
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
Post by Tommy Carstensen
Take a
look and join the conversation now.
http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media,
is
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
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/
Post by Ryan Nelson
Post by Tommy Carstensen
Post by Tommy Carstensen
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
Post by Ryan Nelson
Post by Tommy Carstensen
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
Post by Ryan Nelson
Post by Tommy Carstensen
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Tommy Carstensen
2015-02-14 20:19:47 UTC
Permalink
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:
Loading Image...
Loading Image...

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
Post by Tommy Carstensen
Ryan, do you know, if there is any way I can make the padding
dependent on the tick label sizes?
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.
Post by Ryan Nelson
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
Post by Tommy Carstensen
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.
Tommy,
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
______________
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
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
Post by Tommy Carstensen
Hi Ryan,
Thanks for your answer. Sorry for not replying sooner. I fell asleep
shortly after sending my question.
What is "the OO way"?
AttributeError: 'module' object has no attribute 'ticks'
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
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
set ytics right
Thanks for your help and providing me with a solution.
Tommy
Tommy,
You are probably looking for pyplot.xticks. For example, you might want
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()
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
Post by Tommy Carstensen
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
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
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
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Continue reading on narkive:
Search results for '[Matplotlib-users] horizontal alignment of a secondary y-axis' (Questions and Answers)
6
replies
Is this a good telescope?
started 2011-07-20 13:31:03 UTC
astronomy & space
Loading...