简体   繁体   English

matplotlib两个传说出了情节

[英]matplotlib two legends out of plot

I'm facing problem with showing two legends outside of plot. 我正面临着在情节之外展示两个传说的问题。 Showing multiple legends inside plot is easy - its described in matplotlib doc's with examples. 剧情中显示多个传说很容易 - 它在matplotlib doc中用例子描述。 Even showing one legend outside of plot is rather easy as i found here on stackoverflow (ex. here ). 甚至在情节之外显示一个传说也很容易,就像我在stackoverflow上找到的那样(例如这里 )。 But i cant find working example to show two legends outside of the plot. 但我找不到工作的例子来展示情节之外的两个传说。 Methods which work with one legend is not working in this case. 在这种情况下,使用一个图例的方法不起作用。

Here is an example. 这是一个例子。 First of all base code: 首先是基本代码:

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

But now i want to draw another legend at the right side of plot. 但现在我想在情节的右侧绘制另一个传奇。 Here is the code: 这是代码:

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

...

And the result: 结果如下:

第二,截断的传说

As you can see, second legend is truncated. 如您所见,第二个图例被截断。 My conclusion is that matplotlib ignores size and position of objects added with 我的结论是matplotlib忽略了添加的对象的大小和位置

plt.gca().add_artist(obj)

How can i fix this? 我怎样才能解决这个问题?

So far i found a solution but its very nasty: 到目前为止,我找到了一个解决方案,但它非常讨厌:

Create three legends, two of them as additiontal (added by add_artist) and one as normal legend. 创建三个图例,其中两个作为附加(由add_artist添加)和一个作为普通图例。 As far matplotlib respect position and size of normal legends, move it to the right down corner and hide it with code: 至于matplotlib尊重普通图例的位置和大小,将其移到右下角并用代码隐藏它:

leg.get_frame().set_alpha(0)

Here are the results (without setting alpha for example purpose): 以下是结果(不设置为例如目的的alpha):

情节中的三个传说

It behave exactly how i want it to but as you know its nasty. 它的行为正是我想要的,但正如你所知道的那样令人讨厌。 Here is the final code: 这是最终的代码:

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

I can suggest the following solution: 我可以建议以下解决方案:

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

I used an example from matplotlib site and followed the documentation about tight layout http://matplotlib.org/users/tight_layout_guide.html . 我使用了matplotlib网站上的一个示例,并按照有关紧密布局http://matplotlib.org/users/tight_layout_guide.html的文档进行操作。

The result is 结果是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM