matlab把图例放在左边,如何将图例放在p之外

如何将图例放在p之外

我有一系列20个图(不是子图)可以在一个图中制作。 我希望传说能够超越盒子。 与此同时,我不想改变轴,因为图形的大小减少了。 请帮助我以下查询:

我想将情节框保留在情节区域之外。 (我希望传说位于情节区域的右侧)。

无论如何,我减少了图例框内文本的字体大小,以便图例框的大小很小。

16个解决方案

1360 votes

有很多方法可以做你想要的。 要添加@inalis和@Navi已经说过的内容,可以使用plt.figlegend()关键字参数将图例部分放置在轴外和/或减小字体大小。

在考虑减小字体大小(这可能使事情难以阅读)之前,尝试将图例放在不同的地方:

那么,让我们从一个通用的例子开始:

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend()

plt.show()

LQ8xk.png

如果我们做同样的事情,但使用plt.figlegend()关键字参数,我们可以稍微将图例移到轴边界之外:

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

ax.plot(x, i * x, label='$y = %ix$' % i)

ax.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()

OtE5U.png

同样,你可以使图例更加水平和/或将它放在图的顶部(我也打开圆角和一个简单的阴影):

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

line, = ax.plot(x, i * x, label='$y = %ix$'%i)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),

ncol=3, fancybox=True, shadow=True)

plt.show()

zgtBl.png

或者,您可以缩小当前图的宽度,并将图例完全放在图的轴外:

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

ax.plot(x, i * x, label='$y = %ix$'%i)

# Shrink current axis by 20%

box = ax.get_position()

ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis

ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.show()

v34g8.png

以类似的方式,您可以垂直缩小绘图,并将水平图例放在底部:

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

line, = ax.plot(x, i * x, label='$y = %ix$'%i)

# Shrink current axis's height by 10% on the bottom

box = ax.get_position()

ax.set_position([box.x0, box.y0 + box.height * 0.1,

box.width, box.height * 0.9])

# Put a legend below current axis

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),

fancybox=True, shadow=True, ncol=5)

plt.show()

cXcYa.png

看看matplotlib图例指南。 您也可以看一下plt.figlegend().希望这有点帮助,无论如何!

Joe Kington answered 2019-01-16T21:34:12Z

403 votes

放置图例(loc)

使用loc的loc参数将图例定位在轴的边界框内。

例如。 lax将图例放置在边界框的右上角,默认情况下,在轴坐标(或边界框表示法h,l = ax.get_legend_handles_labels())中,范围从gridspec_kw={"width_ratios":[4,1]}到lax.axis("off")。

要将图例放置在轴边界框之外,可以指定图例左下角的轴坐标的元组loc。

plt.legend(loc=(1.04,0))

但是,更通用的方法是使用loc参数手动指定应放置图例的边界框。 人们可以限制自己只提供bbox的loc部分。 这将创建一个零跨度框,其中图例将按照lax参数指定的方向展开。 例如。

plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")

将图例放置在轴外,使图例的左上角位于轴坐标中的位置loc。

下面给出了进一步的示例,其中另外示出了诸如loc和lax之类的不同参数之间的相互作用。

OIMyM.png

l1 = plt.legend(bbox_to_anchor=(1.04,1), borderaxespad=0)

l2 = plt.legend(bbox_to_anchor=(1.04,0), loc="lower left", borderaxespad=0)

l3 = plt.legend(bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0)

l4 = plt.legend(bbox_to_anchor=(0,1.02,1,0.2), loc="lower left",

mode="expand", borderaxespad=0, ncol=3)

l5 = plt.legend(bbox_to_anchor=(1,0), loc="lower right",

bbox_transform=fig.transFigure, ncol=3)

l6 = plt.legend(bbox_to_anchor=(0.4,0.8), loc="upper right")

有关如何将4元组参数解释为loc的详细信息,如lax,可以在此问题中找到。 gridspec_kw={"width_ratios":[4,1]}在4元组给出的边界框内水平扩展图例。 对于垂直展开的图例,请参阅此问题。

有时,在图形坐标中指定边界框而不是轴坐标可能很有用。 这在上面的示例loc中显示,其中lax参数用于将图例放在图的左下角。

后期处理

将图例放置在轴外部通常会导致不希望的情况,即它完全或部分位于图形画布之外。

这个问题的解决方案是:

调整子图参数

可以通过使用loc来调整子图参数,使得轴在图中占用较少的空间(从而为图例留出更多空间)。

loc

在图的右侧留下30%的空间,可以放置图例。

布局紧凑

使用loc允许自动调整子图参数,使图中的元素紧贴图形边缘。 不幸的是,在这种自动化中没有考虑图例,但我们可以提供一个矩形框,整个子图区域(包括标签)将适合。

loc

用loc保存该图

参数loc至loc可用于保存图形,使得画布上的所有艺术家(包括图例)都适合保存的区域。 如果需要,可自动调整图形大小。

loc

自动调整subplot params

在这个答案中可以找到一种自动调整子图位置以使图例适合画布而不改变图形大小的方法:创建具有精确尺寸且没有填充的图形(以及轴外的图例)

上述案例之间的比较:

zqKjY.png

备择方案

图传说

可以使用图例而不是轴的图例,loc。这对于matplotlib版本> = 2.1特别有用,其中不需要特殊参数

fig.legend(loc=7)

为图中不同轴的所有艺术家创建一个图例。 使用loc参数放置图例,类似于它放置在轴内的方式,但是参考整个图形 - 因此它会稍微自动地在轴外部。 剩下的就是调整子图,使图例和轴之间没有重叠。 从上面的“调整子图参数”这一点将会很有帮助。 一个例子:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0,2*np.pi)

colors=["#7aa0c4","#ca82e1" ,"#8bcd50","#e18882"]

fig, axes = plt.subplots(ncols=2)

for i in range(4):

axes[i//2].plot(x,np.sin(x+i), color=colors[i],label="y=sin(x+{})".format(i))

fig.legend(loc=7)

fig.tight_layout()

fig.subplots_adjust(right=0.75)

plt.show()

v1AU6.png

专用子图轴内的图例

使用loc的替代方法是将图例放置在其专用子图轴(lax)中。由于图例子图应小于图,我们可以在创建轴时使用gridspec_kw={"width_ratios":[4,1]}。我们可以隐藏轴lax.axis("off")但仍然放入图例。图例处理和标签需要通过h,l = ax.get_legend_handles_labels()从真实图中获得,然后可以提供给lax子图lax.legend(h,l)中的图例。下面是一个完整的示例。

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = 6,2

fig, (ax,lax) = plt.subplots(ncols=2, gridspec_kw={"width_ratios":[4,1]})

ax.plot(x,y, label="y=sin(x)")

....

h,l = ax.get_legend_handles_labels()

lax.legend(h,l, borderaxespad=0)

lax.axis("off")

plt.tight_layout()

plt.show()

这会产生一个视觉上与上图相似的情节:

4RrYb.png

我们也可以使用第一个轴来放置图例,但是使用图例轴的loc,

ax.legend(bbox_to_anchor=(0,0,1,1), bbox_transform=lax.transAxes)

lax.axis("off")

在这种方法中,我们不需要从外部获取图例句柄,但我们需要指定loc参数。

进一步阅读和说明:

考虑一下matplotlib图例指南,以及一些你想用传说做的其他东西的例子。

可以直接在回答这个问题时找到用于为饼图放置图例的一些示例代码:Python - 图例与饼图重叠

loc参数可以使用数字而不是字符串,这会使调用更短,但是,它们不是非常直观地相互映射。 以下是参考的映射:

jxecX.png

ImportanceOfBeingErnest answered 2019-01-16T21:38:36Z

115 votes

只需拨打legend()致电plot()之后拨打电话:

# matplotlib

plt.plot(...)

plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))

# Pandas

df.myCol.plot().legend(loc='center left', bbox_to_anchor=(1, 0.5))

结果看起来像这样:

5fgie.png

ShitalShah answered 2019-01-16T21:39:05Z

90 votes

创建字体属性

from matplotlib.font_manager import FontProperties

fontP = FontProperties()

fontP.set_size('small')

legend([plot1], "title", prop=fontP)

Navi answered 2019-01-16T21:32:56Z

73 votes

简答:您可以使用fig.savefig() + bbox_inches + savefig。

更长的答案:您可以使用fig.savefig()手动指定图例框的位置,正如其他人在答案中指出的那样。

但是,通常的问题是图例框被裁剪,例如:

import matplotlib.pyplot as plt

# data

all_x = [10,20,30]

all_y = [[1,3], [1.5,2.9],[3,2]]

# Plot

fig = plt.figure(1)

ax = fig.add_subplot(111)

ax.plot(all_x, all_y)

# Add legend, title and axis labels

lgd = ax.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='center right', bbox_to_anchor=(1.3, 0.5))

ax.set_title('Title')

ax.set_xlabel('x label')

ax.set_ylabel('y label')

fig.savefig('image_output.png', dpi=300, format='png')

DWe9y.png

为了防止图例框被裁剪,当您保存图形时,您可以使用参数fig.savefig()和bbox_inches来询问savefig以在保存的图像中包含裁剪的元素:

fig.savefig()

示例(我只更改了最后一行,将2个参数添加到fig.savefig()):

import matplotlib.pyplot as plt

# data

all_x = [10,20,30]

all_y = [[1,3], [1.5,2.9],[3,2]]

# Plot

fig = plt.figure(1)

ax = fig.add_subplot(111)

ax.plot(all_x, all_y)

# Add legend, title and axis labels

lgd = ax.legend( [ 'Lag ' + str(lag) for lag in all_x], loc='center right', bbox_to_anchor=(1.3, 0.5))

ax.set_title('Title')

ax.set_xlabel('x label')

ax.set_ylabel('y label')

fig.savefig('image_output.png', dpi=300, format='png', bbox_extra_artists=(lgd,), bbox_inches='tight')

xGq1Y.png

我希望matplotlib本身允许传说框的外部位置,如Matlab所做的那样:

figure

x = 0:.2:12;

plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));

hleg = legend('First','Second','Third',...

'Location','NorthEastOutside')

% Make the text of the legend italic and color it brown

set(hleg,'FontAngle','italic','TextColor',[.3,.2,.1])

bUGFR.png

Franck Dernoncourt answered 2019-01-16T21:40:07Z

51 votes

简答:在图例上调用draggable并以交互方式将其移动到任何您想要的位置:

ax.legend().draggable()

长答案:如果您更喜欢以交互方式/手动方式而不是以编程方式放置图例,则可以切换图例的可拖动模式,以便将其拖动到任何您想要的位置。 请查看以下示例:

import matplotlib.pylab as plt

import numpy as np

#define the figure and get an axes instance

fig = plt.figure()

ax = fig.add_subplot(111)

#plot the data

x = np.arange(-5, 6)

ax.plot(x, x*x, label='y = x^2')

ax.plot(x, x*x*x, label='y = x^3')

ax.legend().draggable()

plt.show()

mefathy answered 2019-01-16T21:40:36Z

48 votes

除了这里的所有优秀答案之外,matplotlib和pylab的新版本可以自动确定放置图例的位置而不会干扰图表。

pylab.legend(loc='best')

这将自动将图例放在图表之外!

NDq48.png

dotancohen answered 2019-01-16T21:41:04Z

48 votes

要将图例放置在绘图区域之外,请使用legend()的loc和bbox_to_anchor关键字。 例如,以下代码将图例放置在绘图区域的右侧:

legend(loc="upper left", bbox_to_anchor=(1,1))

有关详细信息,请参阅图例指南

Christian Alis answered 2019-01-16T21:41:33Z

13 votes

不完全是你要求的,但我发现它是同一问题的替代方案。让传奇半透明,如下:

foCZw.png

这样做:

fig = pylab.figure()

ax = fig.add_subplot(111)

ax.plot(x,y,label=label,color=color)

# Make the legend transparent:

ax.legend(loc=2,fontsize=10,fancybox=True).get_frame().set_alpha(0.5)

# Make a transparent text box

ax.text(0.02,0.02,yourstring, verticalalignment='bottom',

horizontalalignment='left',

fontsize=10,

bbox={'facecolor':'white', 'alpha':0.6, 'pad':10},

transform=self.ax.transAxes)

Bastiaan answered 2019-01-16T21:42:02Z

7 votes

如上所述,您还可以将图例放置在绘图中,或稍微偏离边缘。 下面是使用Plotly Python API的示例,该API由IPython Notebook完成。 我在团队中。

首先,您需要安装必要的软件包:

import plotly

import math

import random

import numpy as np

然后,安装Plotly:

un='IPython.Demo'

k='1fw3zw2o13'

py = plotly.plotly(username=un, key=k)

def sin(x,n):

sine = 0

for i in range(n):

sign = (-1)**i

sine = sine + ((x**(2.0*i+1))/math.factorial(2*i+1))*sign

return sine

x = np.arange(-12,12,0.1)

anno = {

'text': '$\\sum_{k=0}^{\\infty} \\frac {(-1)^k x^{1+2k}}{(1 + 2k)!}$',

'x': 0.3, 'y': 0.6,'xref': "paper", 'yref': "paper",'showarrow': False,

'font':{'size':24}

}

l = {

'annotations': [anno],

'title': 'Taylor series of sine',

'xaxis':{'ticks':'','linecolor':'white','showgrid':False,'zeroline':False},

'yaxis':{'ticks':'','linecolor':'white','showgrid':False,'zeroline':False},

'legend':{'font':{'size':16},'bordercolor':'white','bgcolor':'#fcfcfc'}

}

py.iplot([{'x':x, 'y':sin(x,1), 'line':{'color':'#e377c2'}, 'name':'$x\\\\$'},\

{'x':x, 'y':sin(x,2), 'line':{'color':'#7f7f7f'},'name':'$ x-\\frac{x^3}{6}$'},\

{'x':x, 'y':sin(x,3), 'line':{'color':'#bcbd22'},'name':'$ x-\\frac{x^3}{6}+\\frac{x^5}{120}$'},\

{'x':x, 'y':sin(x,4), 'line':{'color':'#17becf'},'name':'$ x-\\frac{x^5}{120}$'}], layout=l)

这将创建您的图形,并允许您有机会将图例保留在图表中。 如果未设置,则图例的默认值是将其放置在图中,如此处所示。

SZgAG.png

对于替代放置,您可以紧密对齐图形的边缘和图例的边框,并删除边框线以便更贴合。

Ev3xA.png

您可以使用代码或GUI移动和重新设置图例和图形的样式。 要移动图例,可以使用以下选项通过指定< = 1的x和y值来将图例放置在图表中.E.g:

legendstyle = {"x" : 1, "y" : 1} - 左下角

legendstyle = {"x" : 1, "y" : 1} - 右下角

legendstyle = {"x" : 1, "y" : 1} - 右上角

legendstyle = {"x" : 1, "y" : 1} - 左上角

legendstyle = {"x" : 1, "y" : 1} - 底部中心

legendstyle = {"x" : 1, "y" : 1} - 顶级中心

在这种情况下,我们选择右上角,legendstyle = {"x" : 1, "y" : 1},也在文档中描述:

O8vCG.png

Mateo Sanchez answered 2019-01-16T21:43:44Z

3 votes

这些方面的东西对我有用。 从Joe获取的一些代码开始,此方法修改窗口宽度以自动将图例放在图的右侧。

import matplotlib.pyplot as plt

import numpy as np

plt.ion()

x = np.arange(10)

fig = plt.figure()

ax = plt.subplot(111)

for i in xrange(5):

ax.plot(x, i * x, label='$y = %ix$'%i)

# Put a legend to the right of the current axis

leg = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

plt.draw()

# Get the ax dimensions.

box = ax.get_position()

xlocs = (box.x0,box.x1)

ylocs = (box.y0,box.y1)

# Get the figure size in inches and the dpi.

w, h = fig.get_size_inches()

dpi = fig.get_dpi()

# Get the legend size, calculate new window width and change the figure size.

legWidth = leg.get_window_extent().width

winWidthNew = w*dpi+legWidth

fig.set_size_inches(winWidthNew/dpi,h)

# Adjust the window size to fit the figure.

mgr = plt.get_current_fig_manager()

mgr.window.wm_geometry("%ix%i"%(winWidthNew,mgr.window.winfo_height()))

# Rescale the ax to keep its original size.

factor = w*dpi/winWidthNew

x0 = xlocs[0]*factor

x1 = xlocs[1]*factor

width = box.width*factor

ax.set_position([x0,ylocs[0],x1-x0,ylocs[1]-ylocs[0]])

plt.draw()

Martin answered 2019-01-16T21:44:06Z

2 votes

您也可以尝试figlegend.可以创建独立于任何Axes对象的图例。 但是,您可能需要创建一些“虚拟”路径以确保正确传递对象的格式。

Uri Laserson answered 2019-01-16T21:44:29Z

1 votes

以下是此处的matplotlib教程中的示例。 这是更简单的示例之一,但我在图例中添加了透明度并添加了plt.show(),因此您可以将其粘贴到交互式shell中并获得结果:

import matplotlib.pyplot as plt

p1, = plt.plot([1, 2, 3])

p2, = plt.plot([3, 2, 1])

p3, = plt.plot([2, 3, 1])

plt.legend([p2, p1, p3], ["line 1", "line 2", "line 3"]).get_frame().set_alpha(0.5)

plt.show()

radtek answered 2019-01-16T21:44:50Z

1 votes

当我有一个巨大的传奇时,对我有用的解决方案是使用额外的空图像布局。在下面的示例中,我制作了4行,在底部我绘制了带有图例偏移的图像(bbox_to_anchor),但是它没有被剪切。

f = plt.figure()

ax = f.add_subplot(414)

lgd = ax.legend(loc='upper left', bbox_to_anchor=(0, 4), mode="expand", borderaxespad=0.3)

ax.autoscale_view()

plt.savefig(fig_name, format='svg', dpi=1200, bbox_extra_artists=(lgd,), bbox_inches='tight')

Crystal answered 2019-01-16T21:45:13Z

0 votes

这是另一种解决方案,类似于添加bbox_extra_artists和bbox_inches,您无需在savefig电话的范围内拥有额外的艺术家。 我想出了这个,因为我在函数中生成了大部分情节。

您可以提前将它们添加到Figure的艺术家,而不是将所有添加内容添加到边界框中。 使用类似于上面的Franck Dernoncourt的答案:

import matplotlib.pyplot as plt

# data

all_x = [10,20,30]

all_y = [[1,3], [1.5,2.9],[3,2]]

# plotting function

def gen_plot(x, y):

fig = plt.figure(1)

ax = fig.add_subplot(111)

ax.plot(all_x, all_y)

lgd = ax.legend( [ "Lag " + str(lag) for lag in all_x], loc="center right", bbox_to_anchor=(1.3, 0.5))

fig.artists.append(lgd) # Here's the change

ax.set_title("Title")

ax.set_xlabel("x label")

ax.set_ylabel("y label")

return fig

# plotting

fig = gen_plot(all_x, all_y)

# No need for `bbox_extra_artists`

fig.savefig("image_output.png", dpi=300, format="png", bbox_inches="tight")

这是生成的图。

ivirshup answered 2019-01-16T21:45:49Z

0 votes

不知道你是否已经解决了你的问题...可能是的,但......我只是使用字符串'outside'作为位置,就像在matlab中一样。我从matplotlib导入了pylab。看到代码如下:

from matplotlib as plt

from matplotlib.font_manager import FontProperties

...

...

t = A[:,0]

sensors = A[:,index_lst]

for i in range(sensors.shape[1]):

plt.plot(t,sensors[:,i])

plt.xlabel('s')

plt.ylabel('°C')

lgd = plt.legend(b,loc='center left', bbox_to_anchor=(1, 0.5),fancybox = True, shadow = True)

点击查看情节

Ziuccia answered 2019-01-16T21:46:18Z

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值