# unwanted label clipping in gallery

Here is another example of unwanted text clipping
in the gallery:
http://matplotlib.sourceforge.net/examples/api/two_scales.html#api-two-scales
(Both y axis labels are clipped.)

I also think the example would be more complete if it
1. set a 270 degree rotation on the second ylabel, and
2. showed how to make a single legend for the two lines

Btw, how *does* one best do 2?

Cheers,
Alan Isaac

Alan G Isaac, on 2010-10-27 18:31, wrote:

Here is another example of unwanted text clipping
in the gallery:
http://matplotlib.sourceforge.net/examples/api/two_scales.html#api-two-scales
(Both y axis labels are clipped.)

I also think the example would be more complete if it
1. set a 270 degree rotation on the second ylabel, and
2. showed how to make a single legend for the two lines

Btw, how *does* one best do 2?

I don't know if it's best, but legend can take a list of objects
and labels, so you can just grab all of the objects from the twin,
and put them all in one legend:

def onelegend_twinaxes(axis,twin):
#make a joint axis legend
lines = twin.get_lines()
lines.extend(axis.get_lines())
labels = [l.get_label() for l in lines]
return axis.legend(lines, labels)

Here's a picture of what that looks like (thought I did some
other prettifications).
<http://pirsquared.org/images/twinaxes_onelegend.png>

I wrote this in a solution set for a class I'm TAing this
semester, so you can look at the whole thing here, if you'd like.
the file is part of the solutions for Lab #1, it's called lab1.py
<http://redwood.berkeley.edu/wiki/VS265:_Homework_assignments>

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

For this example, saving the line objects will do. Then you just call
legend with the objects. The new example looks thusly:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
line1 = ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')

ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
line2 = ax2.plot(t, s2, 'r.')
# Rotate ylabel 180 from normal y-axis label orientation
ax2.set_ylabel('sin', color='r', rotation=270.)
for tl in ax2.get_yticklabels():
tl.set_color('r')

ax2.legend((line1, line2), ('exp(t)', '$sin(2 \pi t)$'))
plt.show()

Thanks for the suggestions. Any idea how the clipped figure problem
was solved in the past?

Ryan

···

On Wed, Oct 27, 2010 at 5:31 PM, Alan G Isaac <aisaac@...310...> wrote:

Here is another example of unwanted text clipping
in the gallery:
http://matplotlib.sourceforge.net/examples/api/two_scales.html#api-two-scales
(Both y axis labels are clipped.)

I also think the example would be more complete if it
1. set a 270 degree rotation on the second ylabel, and
2. showed how to make a single legend for the two lines

Btw, how *does* one best do 2?

--
Ryan May
School of Meteorology
University of Oklahoma

http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg18632.html

http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg18537.html

fwiw,
Alan

···

On 10/27/2010 9:56 PM, Ryan May wrote:

Any idea how the clipped figure problem
was solved in the past?

def onelegend_twinaxes(axis,twin):
#make a joint axis legend
lines = twin.get_lines()
lines.extend(axis.get_lines())
labels = [l.get_label() for l in lines]
return axis.legend(lines, labels)

That works.

Cool. What proportion of the students choose Python?

Cheers,
Alan

···

On 10/27/2010 8:21 PM, Paul Ivanov wrote:

Thanks. This is cool, but I think I find the colored tick
labels visually distracting. Isn't a colored axis label enough?

Cheers,
Alan

···

On 10/27/2010 9:56 PM, Ryan May wrote:

# Make the y-axis label and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')

Alan G Isaac, on 2010-10-28 21:29, wrote:

···

On 10/27/2010 8:21 PM, Paul Ivanov wrote:
> def onelegend_twinaxes(axis,twin):
> #make a joint axis legend
> lines = twin.get_lines()
> lines.extend(axis.get_lines())
> labels = [l.get_label() for l in lines]
> return axis.legend(lines, labels)

That works.

Cool. What proportion of the students choose Python?

This is the first time that Python's been officially encouraged,
and it's been about 1 in 6.

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7