所以我现在正在用matplotlib进入更复杂的图形,并试图使用下面的图和代码。matplotlib - 具有不同基本指数和图例的次Y轴
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()
我的两个问题是:
- 我觉得我已经做了基本指数改进剂以一种奇怪的方式,因为这样的事实,我有两个Y轴。有没有更好的方法来做双轴图,因为它好像是我把它直接放在轴的.plot后面。
- 另外,因为我有两个独立的轴,我不能让图例工作。
我已经尝试了类似的问题,从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()
回答以下因此,LNS在我的代码。但是每次我以这种方式运行时,Pylab都会崩溃而不会出现错误。
任何人都可以帮助解决这些问题吗?
编辑:这是我得到的错误,当我使用完全相同的代码从macduff的答复复制,这是我提到的代码相同。
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),))
我遵守这一点,它没有工作。这个传说只是表现为一个没有任何东西的小广场。但是,我注意到我收到了一种错误。我会用它更新原始问题。 – 2012-03-02 21:02:23
迟到的反应迟了,但最终确实发生了这个问题,与我相信的乳胶字符无关。 – 2012-05-13 08:10:25