2012-03-02 39 views
2

所以我現在正在用matplotlib進入更復雜的圖形,並試圖使用下面的圖和代碼。matplotlib - 具有不同基本指數和圖例的次Y軸

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() 

我的兩個問題是:

  1. 我覺得我已經做了基本指數改進劑以一種奇怪的方式,因爲這樣的事實,我有兩個Y軸。有沒有更好的方法來做雙軸圖,因爲它好像是我把它直接放在軸的.plot後面。
  2. 另外,因爲我有兩個獨立的軸,我不能讓圖例工作。

我已經嘗試了類似的問題,從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),)) 

回答

1

所以,看起來你很不高興的是,傳說不符合你的喜好。 當我拿着你的例程,添加一些測試數據省略;-),並添加一個 的傳說,我沒有任何問題。讓我知道我如何能幫助弄清這個問題, 。

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() 

我會在稍後添加我的輸出圖。

+0

我遵守這一點,它沒有工作。這個傳說只是表現爲一個沒有任何東西的小廣場。但是,我注意到我收到了一種錯誤。我會用它更新原始問題。 – 2012-03-02 21:02:23

+0

遲到的反應遲了,但最終確實發生了這個問題,與我相信的乳膠字符無關。 – 2012-05-13 08:10:25

0

傳說是窗口蟒蛇相關軸(我猜不中的* nix或OSX版本?)。如果我使用macduff的示例代碼,我會得到與T相同的錯誤。

但是,如果我在fig2 = fig1.twinx()之前和pylab.show()之前繪製圖例,我會得到兩個圖例框和代碼。

因此,在Windows中,每個軸都有自己的圖例(bug或特徵?),並且如果您嘗試爲一個plot繪製3個標籤,則會變得混亂。我猜想在這種情況下,它會嘗試重新塑造legend()參數。我的意思是(lns1, lns2, lns3), ('Plain Line', 'V_t', 'V_t Error')恰好也是一個1乘2的數組元組,ax2有一個圖。所以它認爲列表中的第一項是標籤文本,第二項是位置。

下面是一個「工作」解決方案。如果你想讓文本在同一個盒子上,首先製作一個虛擬繪圖,使其與第二個軸的顏色相同,然後繪製第一個圖例(我知道,它是一個黑客)。

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() 
0

爲了避免錯誤,並顯示所有的次要情節中一個傳奇,使用元組轉換:

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() 

它在Python 2.7.6對我的作品在Linux上。