2016-03-01 124 views
2

我正面临着在剧情之外显示两个传说的问题。 显示多个传说里面情节很简单 - 它在matplotlib文档中的例子中描述。 即使在剧情之外显示一个传说也相当容易,因为我在这里找到了stackoverflow(例如here)。 但我找不到工作的例子来显示剧情之外的两个传说。 使用一个图例的方法在这种情况下不起作用。matplotlib两个传说出来的阴谋

这里是一个例子。 首先基本代码:

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
from matplotlib.lines import Line2D 
from matplotlib.font_manager import FontProperties 

fig1 = plt.figure(figsize=(17,5)) 
fontP = FontProperties() 
fontP.set_size('small') 
ax1 = fig1.add_subplot(111, aspect='equal') 
ax1.grid() 


# stuff for legend 
rec1 = patches.Rectangle(
    (0.9, 0.25), # (x,y) 
    0.1,   # width 
    0.1,   # height 
    label='rectangle', 
    **{ 
     'color': 'blue' 
    } 

) 
ax1.add_patch(rec1) 

leg = plt.legend(handles=[rec1], bbox_to_anchor=(0.7, -0.1)) 
fig1.savefig('sample1.png', dpi=90, bbox_inches='tight') 

但现在我想在曲线的右侧画出另一个传奇。 下面是代码:

... 
ax1.add_patch(rec1) 

l1 = plt.legend(prop=fontP, handles=[rec1], loc='center left', 
       box to_anchor=(1.0, 0.5)) 
plt.gca().add_artist(l1) 

... 

而结果:

Second, truncated legend

正如你所看到的,第二个传说被截断。 我的结论是matplotlib忽略与

plt.gca().add_artist(obj) 

我怎样才能解决这个添加对象的大小和位置?

到目前为止,我发现了一个解决方案,但它很讨厌:

创建三个传说,两人对视additiontal(由add_artist加)和一个正常的传说。 至于matplotlib尊重位置和法线传说的大小,将其移动到右下角和隐藏与代码它:

leg.get_frame().set_alpha(0) 

下面是结果(不设置例如目的阿尔法):

Three legends out of plot

它的行为完全是我想要的,但正如你知道它的讨厌。 下面是最终的代码:

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
from matplotlib.lines import Line2D 
from matplotlib.font_manager import FontProperties 

fig1 = plt.figure(figsize=(17,5)) 
fontP = FontProperties() 
fontP.set_size('small') 
ax1 = fig1.add_subplot(111, aspect='equal') 
ax1.grid() 

# stuff for additional legends 
rec1 = patches.Rectangle(
    (0.9, 0.25), # (x,y) 
    0.1,   # width 
    0.1,   # height 
    label='rectangle', 
    **{ 
     'color': 'blue' 
    } 
) 
ax1.add_patch(rec1) 

# example additional legends 
l1 = plt.legend(prop=fontP, handles=[rec1], loc='center left', 
bbox_to_anchor=(1.0, 0.5)) 
l2 = plt.legend(prop=fontP, handles=[rec1], loc=3, bbox_to_anchor=(0.4, 
-0.2)) 

# add legends 
plt.gca().add_artist(l1) 
plt.gca().add_artist(l2) 

# add third legend 
leg = plt.legend(handles=[], bbox_to_anchor=(1.3, -0.3)) 
leg.get_frame().set_alpha(0) # hide legend 

fig1.savefig('sample3.png', dpi=90, bbox_inches='tight') 

回答

3

我可以建议如下解决方案:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

fig = plt.figure() 
fig.set_size_inches((10,10)) 

gs1 = gridspec.GridSpec(1, 1) 
ax1 = fig.add_subplot(gs1[0]) 

x = np.arange(0.0, 3.0, 0.02) 
y1 = np.sin(2*np.pi*x) 
y2 = np.exp(-x) 
l1, l2 = ax1.plot(x, y1, 'rs-', x, y2, 'go') 

y3 = np.sin(4*np.pi*x) 
y4 = np.exp(-2*x) 
l3, l4 = ax1.plot(x, y3, 'yd-', x, y4, 'k^') 

fig.legend((l1, l2), ('Line 1', 'Line 2'), "right") 
fig.legend((l3, l4), ('Line 3', 'Line 4'), "lower center") 

gs1.tight_layout(fig, rect=[0, 0.1, 0.8, 0.5]) 

我使用matplotlib站点的示例,并且随后约紧布局http://matplotlib.org/users/tight_layout_guide.html的文档。

结果是

+0

这样做几乎完美。我只需要对传说位置和情节规模进行一些调整,但它完全符合我的需求。谢谢! –