在matplotlib中移动图例框并调整其大小 [英] Move and resize legends-box in matplotlib

查看:670
本文介绍了在matplotlib中移动图例框并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Matplotlib创建图,并将其另存为SVG,然后使用Inkscape导出到.pdf + .pdf_tex,并将.pdf_tex文件包含在LaTeX文档中.

I'm creating plots using Matplotlib that I save as SVG, export to .pdf + .pdf_tex using Inkscape, and include the .pdf_tex-file in a LaTeX document.

这意味着我可以在标题,图例等中输入LaTeX命令,得到这样的图像

This means that I can input LaTeX-commands in titles, legends etc., giving an image like this

当我在LaTeX文档中使用它时会呈现出这样的效果.请注意,轴上数字的字体会更改,并且图例中的LaTeX代码也会被编译:

which renders like this when I use it in my LaTeX document. Notice that the font for the numbers on the axes change, and the LaTeX-code in the legend is compiled:

图的代码(此处未显示如何导出到SVG,但可以根据要求显示):

Code for the plot (how to export to SVG not shown here, but can be shown on request):

import numpy as np
x = np.linspace(0,1,100)
y = x**2

import matplotlib.pyplot as plt
plt.plot(x, y, label = '{\\footnotesize \$y = x^2\$}')
plt.legend(loc = 'best')
plt.show()

问题是,如您所见,图例周围的框的对齐方式和大小是错误的.这是因为当图像通过Inkscape + pdflatex时,标签文本的大小会发生变化(因为\footnotesize等消失,字体大小也会发生变化).

The problem is, as you can see, that the alignment and size of the box around the legend is wrong. This is because the size of the text of the label changes when the image is passed through Inkscape + pdflatex (because \footnotesize etc. disappears, and the font size changes).

我发现可以选择其中一个标签的位置

I have figured out that I can choose the placement of the label by either

plt.label(loc = 'upper right')

或者,如果我想要更多控制权,可以使用

or if I want more control I can use

plt.label(bbox_to_anchor = [0.5, 0.2])

但是我还没有找到使标签周围的盒子变小的任何方法.这可能吗?

but I haven't found any way of making the box around the label smaller. Is this possible?

缩小框的另一种方法是使用类似的方法删除框的轮廓

An alternative to making the box smaller is to remove the outline of the box using something like

legend = plt.legend()
legend.get_frame().set_edgecolor('1.0')

,然后将标签移动到我想要的位置.在那种情况下,我希望能够通过首先让python/matplotlib使用来设置标签的位置

and then moving the label to where I want it. In that case I would like to be able to set the placement of the label by first letting python/matplotlib place it using

plt.label(loc = 'upper right')

,然后将其向右移动一点.这可能吗?我曾尝试使用get_bbox_to_anchor()set_bbox_to_anchor(),但似乎无法使其正常工作.

and then for example moving it a bit to the right. Is this possible? I have tried using get_bbox_to_anchor() and set_bbox_to_anchor(), but can't seem to get it to work.

推荐答案

在自动放置图例后,可以通过绘制图例,然后获取bbox的位置来移动图例.这是一个示例:

You can move a legend after automatically placing it by drawing it, and then getting the bbox position. Here's an example:

import matplotlib.pyplot as plt
import numpy as np

# Plot data
x = np.linspace(0,1,100)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(221) #small subplot to show how the legend has moved. 

# Create legend
plt.plot(x, y, label = '{\\footnotesize \$y = x^2\$}')
leg = plt.legend( loc = 'upper right')

plt.draw() # Draw the figure so you can find the positon of the legend. 

# Get the bounding box of the original legend
bb = leg.get_bbox_to_anchor().inverse_transformed(ax.transAxes)

# Change to location of the legend. 
xOffset = 1.5
bb.x0 += xOffset
bb.x1 += xOffset
leg.set_bbox_to_anchor(bb, transform = ax.transAxes)


# Update the plot
plt.show()

>

这篇关于在matplotlib中移动图例框并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆