So I am now getting into more complicated graphs with matplotlib, and am trying to work with the following plot and code.
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:
- 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.
- 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),))