2012-03-02 98 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上。