所以我現在正在用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