2

So I am now getting into more complicated graphs with matplotlib, and am trying to work with the following plot and code.

enter image description here

fig_base = pylab.figure()
fig1 = fig_base.add_subplot(111)
lns1 = fig1.plot(manXnames, Ynames, marker='s', color='g')
lns2 = fig1.plot(Vt_X, Vt_Y, marker='^', color='r')

# yticks on left
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs*1e3))

#axis labels    
pylab.xlabel('Temperature (C)') 
pylab.ylabel('Emitter Voltage (mV)', labelpad=20)
pylab.xticks(rotation=45)

fig2 = fig1.twinx()
lns3 = fig2.plot(manXnames, Vterror, marker='o', linestyle='-') 

# xticks
locs,labels = xticks()
pylab.xticks(locs, map(lambda x: "%g" % x, locs))

# yticks on right
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs))

#2nd axis labels    
pylab.ylabel('Percentage Error %', labelpad=20)
pylab.show()

My two problems are as follows:

  1. I think I have done the base exponent modifiers in a weird way, due to the fact that I have two y axes. Is there a better way to do dual axis plots because it seems like I just hacked it by putting it straight after the .plot of the axis.
  2. Also because I have two seperate axes, I can't get the legend to work.

I have tried a similar questions answer below from here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure()
ax = fig.add_subplot(111)

lns1 = ax.plot(time, Swdown, '-', label = 'Swdown')
lns2 = ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
lns3 = ax2.plot(time, temp, '-r', label = 'temp')

lns = lns1+lns2+lns3
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc=0)

ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.show()

Hence the lns in my code. But everytime I run this way, Pylab crashes without giving an error.

Can anyone help with these problems?

EDIT: This is the error I get when I use the exact same code copied from macduff's reply, which is the same as the code I mentioned.

In [16]: te.test()

C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
 not support [<matplotlib.lines.Line2D object at 0x04F8D410>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))
C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
 not support [<matplotlib.lines.Line2D object at 0x04F8D630>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))
C:\Python27\lib\site-packages\matplotlib\legend.py:610: UserWarning: Legend does
 not support [<matplotlib.lines.Line2D object at 0x04FB4D90>]
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp:/
/matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str
(orig_handle),))
Community
  • 1
  • 1
T May
  • 466
  • 1
  • 8
  • 18

3 Answers3

2

So, it seems you're upset that the legend doesn't work the way you would like. When I take your routine, add some test data that you omitted ;-), and add a legend, I get no problems. Let me know how I can help to get this straightened out.

import pylab
from pylab import *
import numpy as np

manXnames = np.array(range(0,120))
Ynames = (8.3333333333333331e-05)*manXnames + 0.01
Vt_X = manXnames
Vt_Y = (12.0/1000.0)*np.exp(-0.01*(120-manXnames))
Vterror = Ynames + randn(size(Ynames))

fig_base = pylab.figure()
fig1 = fig_base.add_subplot(111)
lns1 = fig1.plot(manXnames, Ynames, marker='s', color='g',label='Plain Line')
lns2 = fig1.plot(Vt_X, Vt_Y, marker='^', color='r',label='V_t')

# yticks on left
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs*1e3))

#axis labels
pylab.xlabel('Temperature (C)') 
pylab.ylabel('Emitter Voltage (mV)', labelpad=20)
pylab.xticks(rotation=45)

fig2 = fig1.twinx()
lns3 = fig2.plot(manXnames, Vterror, marker='o', linestyle='-',label='V_terror') 

# xticks
locs,labels = xticks()
pylab.xticks(locs, map(lambda x: "%g" % x, locs))

# yticks on right
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs))

#2nd axis labels    
pylab.ylabel('Percentage Error %', labelpad=20)

pylab.legend((lns1, lns2, lns3), ('Plain Line', 'V_t', 'V_t Error'))
pylab.show()

l'll add a plot of my output later.

macduff
  • 4,655
  • 18
  • 29
  • I complied this and it did not work. The legend just shows as a tiny square with nothing in it. However I noticed I am getting a type of error. I will update the original question with it. – T May Mar 02 '12 at 21:02
  • Late response is late, but this did eventually work the problem was something unrelated to do with latex characters I believe. – T May May 13 '12 at 08:10
0

To avoid errors and show all subplots in one legend, use tuple converting:

lns1 = tuple(fig1.plot(manXnames, Ynames, marker='s', color='g',label='Plain Line')) 
lns2 = tuple(fig1.plot(Vt_X, Vt_Y, marker='^', color='r',label='V_t'))
fig2 = fig1.twinx()
lns3 = tuple(fig2.plot(manXnames, Vterror, marker='o', linestyle='-',label='V_terror'))
pylab.legend((lns1, lns2, lns3), ('Plain Line', 'V_t', 'V_t Error'))
pylab.show()

It works for me on Linux in Python 2.7.6.

Corvax
  • 782
  • 8
  • 13
0

Legends are axis related in windows python (I guess not in *nix or osx version?). I get the same error as T May if I use macduff's sample code.

But if I plot legends before the line fig2 = fig1.twinx() and before pylab.show() I get two legend boxes and the code works.

So, in windows each axis has its own legend (bug or a feature?) and if you try to have 3 labels for one plot, it gets messy. I would guess that in this case it tries to reshape the legend() parameters. I mean (lns1, lns2, lns3), ('Plain Line', 'V_t', 'V_t Error') just happens to be also a 1-by-2 tuple of arrays and ax2 has one plot. So it thinks that the first item in a list is a label text and the second is the location.

Below is a "working" solution. If you want the texts to the same box make a dummy plot to first axis outside the view area with the same colors as in the second axis and just plot the first legend (I know, its a hack).

import pylab
from pylab import *
import numpy as np

manXnames = np.array(range(0,120))
Ynames = (8.3333333333333331e-05)*manXnames + 0.01
Vt_X = manXnames
Vt_Y = (12.0/1000.0)*np.exp(-0.01*(120-manXnames))
Vterror = Ynames + randn(size(Ynames))

fig_base = pylab.figure()
fig1 = fig_base.add_subplot(111)
lns1 = fig1.plot(manXnames, Ynames, marker='s', color='g',label='Plain Line')
lns2 = fig1.plot(Vt_X, Vt_Y, marker='^', color='r',label='V_t')

# yticks on left
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs*1e3))

#axis labels
pylab.xlabel('Temperature (C)') 
pylab.ylabel('Emitter Voltage (mV)', labelpad=20)
pylab.xticks(rotation=45)

pylab.legend()
fig2 = fig1.twinx()
lns3 = fig2.plot(manXnames, Vterror, marker='o', linestyle='-',label='V_terror') 

# xticks
locs,labels = xticks()
pylab.xticks(locs, map(lambda x: "%g" % x, locs))

# yticks on right
locs,labels = yticks()
pylab.yticks(locs, map(lambda x: "%g" % x, locs))

#2nd axis labels    
pylab.ylabel('Percentage Error %', labelpad=20)

pylab.legend(loc="lower right")
pylab.show()
Juha
  • 2,053
  • 23
  • 44