Using labels with "twinx()"

Please consider:

plot([1, 2, 3, 4], label=‘line 1’)

twinx()
plot([11, 12, 11, 14], label=‘line 2’)
legend()

will draw only label for ‘line 2’

plot([1, 2, 3, 4], label=‘line 1’)

legend()

twinx()

plot([11, 12, 11, 14], label=‘line 2’)

legend()

same result, as it will overwrite label ‘line 1’ with label ‘line 2’

How to deal with this, without manually positioning legends and if possible including all annotated plot lines in one legend?

Thanks

Please consider:

      plot([1,

2, 3, 4], label=‘line 1’)

    twinx()

    plot([11, 12, 11, 14], label='line 2')

    legend()

will draw only label for ‘line 2’

plot([1, 2, 3, 4], label=‘line 1’)

legend()

twinx()

plot([11, 12, 11, 14], label=‘line 2’)

legend()

    same result, as it will overwrite label 'line 1' with label

‘line 2’

    How to deal with this, without manually positioning legends

and if possible including all annotated plot lines in one
legend?

Thanks

I would do something like

from matplotlib import pylab

    LegendText = []



    pylab.twinx() # << had to move before first plot else it

blew up

    pylab.plot([1, 2, 3, 4] )

    LegendText.append('line 1')



    pylab.plot([11, 12, 11, 14])

    LegendText.append('line 2')



    pylab.legend( LegendText , loc='lower right')



    pylab.show()
Don't know if there is a better way

Steve
···

On 28/09/2011 4:32 PM, Klonuo Umom wrote:

Could you file a bug report on this on github? Unfortunately, I think the problem is going to be fairly complicated to solve.

Thanks!
Ben Root

···

On Wednesday, September 28, 2011, Klonuo Umom <klonuo@…287…> wrote:

Please consider:

plot([1, 2, 3, 4], label=‘line 1’)
twinx()

plot([11, 12, 11, 14], label=‘line 2’)
legend()

will draw only label for ‘line 2’

plot([1, 2, 3, 4], label=‘line 1’)
legend()
twinx()

plot([11, 12, 11, 14], label=‘line 2’)
legend()

same result, as it will overwrite label ‘line 1’ with label ‘line 2’
How to deal with this, without manually positioning legends and if possible including all annotated plot lines in one legend?

Thanks

*twinx* creates a new axes. Thus there are TWO axes, and you need to
do some manual adjustment. I believe that the solution suggested by
Stephen George is essentially the best way, although you may try to
tweak things using the Axes.get_legend_handles_labels method (
http://matplotlib.sourceforge.net/users/legend_guide.html#what-to-be-displayed
).

Alternatively, you can try the axes_grid1 toolkit which automatically
merges legends for you. Check out the example below.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#example-1-twinx

Regards,

-JJ

···

On Wed, Sep 28, 2011 at 3:32 PM, Klonuo Umom <klonuo@...287...> wrote:

How to deal with this, without manually positioning legends and if possible
including all annotated plot lines in one legend?

Thanks Stephen, but I’m not sure if I follow correctly: I used twinx() as I wanted “line 1” to be referenced on left Y-axis and “line 2” on right Y-axis. In your example I can’t see what’s the purpose of twinx() command? - It presents left Y-axis as default 0 to 1 values not referenced to any plot.

···

On Wed, Sep 28, 2011 at 9:14 AM, Stephen George <steve_geo@…887…> wrote:

On 28/09/2011 4:32 PM, Klonuo Umom wrote:

Please consider:

      plot([1,

2, 3, 4], label=‘line 1’)

    twinx()

    plot([11, 12, 11, 14], label='line 2')

    legend()

will draw only label for ‘line 2’

plot([1, 2, 3, 4], label=‘line 1’)

legend()

twinx()

plot([11, 12, 11, 14], label=‘line 2’)

legend()

    same result, as it will overwrite label 'line 1' with label

‘line 2’

    How to deal with this, without manually positioning legends

and if possible including all annotated plot lines in one
legend?

Thanks

I would do something like

from matplotlib import pylab

    LegendText = []



    pylab.twinx() # << had to move before first plot else it

blew up

    pylab.plot([1, 2, 3, 4] )

    LegendText.append('line 1')



    pylab.plot([11, 12, 11, 14])

    LegendText.append('line 2')



    pylab.legend( LegendText , loc='lower right')



    pylab.show()
Don't know if there is a better way

Steve

All the data continuously generated in your IT infrastructure contains a

definitive record of customers, application performance, security

threats, fraudulent activity and more. Splunk takes this data and makes

sense of it. Business sense. IT sense. Common sense.

http://p.sf.net/sfu/splunk-d2dcopy1


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

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

Thanks JJ,

`axes_grid1` seems to handle this issue

···

On Wed, Sep 28, 2011 at 4:01 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

On Wed, Sep 28, 2011 at 3:32 PM, Klonuo Umom <klonuo@...287...> wrote:
> How to deal with this, without manually positioning legends and if possible
> including all annotated plot lines in one legend?

*twinx* creates a new axes. Thus there are TWO axes, and you need to
do some manual adjustment. I believe that the solution suggested by
Stephen George is essentially the best way, although you may try to
tweak things using the Axes.get_legend_handles_labels method (
http://matplotlib.sourceforge.net/users/legend_guide.html#what-to-be-displayed
).

Alternatively, you can try the axes_grid1 toolkit which automatically
merges legends for you. Check out the example below.

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#example-1-twinx

Regards,

-JJ