我有一系列20幅图(不是子图)要在一个图中绘制。我希望图例在框外。同时,我不想改变轴,因为图形的大小会变小。
我希望图例框位于绘图区域之外(我希望图例位于绘图区域的右侧)。有没有办法减小图例框内文本的字体大小,使图例框的大小变小?
我有一系列20幅图(不是子图)要在一个图中绘制。我希望图例在框外。同时,我不想改变轴,因为图形的大小会变小。
我希望图例框位于绘图区域之外(我希望图例位于绘图区域的右侧)。有没有办法减小图例框内文本的字体大小,使图例框的大小变小?
当前回答
当我有一个巨大的传奇时,对我有效的解决方案是使用额外的空图像布局。
在下面的示例中,我绘制了四行,在底部绘制了带有图例偏移的图像(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')
其他回答
通过指定FontProperties的set_size,可以使图例文本变小。资源:图例指南matplotlib.legendmatplotlib.pyplot.legend字体管理器set_size(自身,大小)有效字体大小为xx小、x小、小、中、大、x大、xx大、大、小和无。Real Python:使用Matplotlib绘制Python(指南)
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
fontP.set_size('xx-small')
p1, = plt.plot([1, 2, 3], label='Line 1')
p2, = plt.plot([3, 2, 1], label='Line 2')
plt.legend(handles=[p1, p2], title='title', bbox_to_anchor=(1.05, 1), loc='upper left', prop=fontP)
fontsize='xx-small'也可以工作,无需导入FontProperties。
plt.legend(handles=[p1, p2], title='title', bbox_to_anchor=(1.05, 1), loc='upper left', fontsize='xx-small')
当我有一个巨大的传奇时,对我有效的解决方案是使用额外的空图像布局。
在下面的示例中,我绘制了四行,在底部绘制了带有图例偏移的图像(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')
如前所述,您也可以将图例放置在绘图中,或稍微偏离其边缘。下面是一个使用Plotly Python API的示例,该API是用IPython笔记本制作的。我是团队成员。
首先,您需要安装必要的软件包:
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)
这将创建图形,并允许您将图例保留在绘图本身中。如果图例未设置,则默认将其放置在绘图中,如图所示。
对于另一种放置方式,可以紧密对齐图形的边缘和图例的边框,并删除边框线以更紧密地配合。
您可以使用代码或GUI移动图例和图形并重新设置其样式。要移动图例,可以使用以下选项通过指定x和y值<=1来在图形中定位图例。例如:
{“x”:0,“y”:0}--左下{“x”:1,“y”:0}--右下{“x”:1,“y”:1}--右上{“x”:0,“y”:1}--左上{“x”:.5,“y”:0}--底部居中{“x”:.5,“y”:1}--顶部居中
在本例中,我们选择右上角的legendstyle={“x”:1,“y”:1},文档中也有描述:
要将图例放置在绘图区域之外,请使用legend()的loc和bbox_To_anchor关键字。例如,以下代码将图例放置在绘图区域的右侧:
legend(loc="upper left", bbox_to_anchor=(1,1))
有关详细信息,请参阅图例指南
这里有一个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()