在轴外移动 matplotlib 图例使其被图形框截断 [英] Moving matplotlib legend outside of the axis makes it cutoff by the figure box

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

问题描述

我熟悉以下问题:

解决方案

抱歉 EMS,但实际上我刚刚收到了来自 matplotlib 邮件列表的另一个回复(感谢 Benjamin Root).

我正在寻找的代码是将 savefig 调用调整为:

fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight')#注意 bbox_extra_artists 必须是可迭代的

这显然类似于调用tight_layout,但您允许savefig 在计算中考虑额外的艺术家.这实际上确实根据需要调整了图形框的大小.

将 matplotlib.pyplot 导入为 plt将 numpy 导入为 npplt.gcf().clear()x = np.arange(-2*np.pi, 2*np.pi, 0.1)fig = plt.figure(1)ax = fig.add_subplot(111)ax.plot(x, np.sin(x), label='Sine')ax.plot(x, np.cos(x), label='余弦')ax.plot(x, np.arctan(x), label='逆棕褐色')句柄,标签 = ax.get_legend_handles_labels()lgd = ax.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5,-0.1))text = ax.text(-0.2,1.05, "任意文本", transform=ax.transAxes)ax.set_title("三角函数")ax.grid('on')fig.savefig('samplefigure', bbox_extra_artists=(lgd,text), bbox_inches='tight')

这会产生:

这个问题的目的是完全避免使用任意文本的任意坐标放置,因为这是这些问题的传统解决方案.尽管如此,最近的许多编辑都坚持将它们放入其中,通常会导致代码出现错误.我现在已经解决了这些问题并整理了任意文本,以显示如何在 bbox_extra_artists 算法中考虑这些问题.

I'm familiar with the following questions:

Matplotlib savefig with a legend outside the plot

How to put the legend out of the plot

It seems that the answers in these questions have the luxury of being able to fiddle with the exact shrinking of the axis so that the legend fits.

Shrinking the axes, however, is not an ideal solution because it makes the data smaller making it actually more difficult to interpret; particularly when its complex and there are lots of things going on ... hence needing a large legend

The example of a complex legend in the documentation demonstrates the need for this because the legend in their plot actually completely obscures multiple data points.

http://matplotlib.sourceforge.net/users/legend_guide.html#legend-of-complex-plots

What I would like to be able to do is dynamically expand the size of the figure box to accommodate the expanding figure legend.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
lgd = ax.legend(loc=9, bbox_to_anchor=(0.5,0))
ax.grid('on')

Notice how the final label 'Inverse tan' is actually outside the figure box (and looks badly cutoff - not publication quality!)

Finally, I've been told that this is normal behaviour in R and LaTeX, so I'm a little confused why this is so difficult in python... Is there a historical reason? Is Matlab equally poor on this matter?

I have the (only slightly) longer version of this code on pastebin http://pastebin.com/grVjc007

解决方案

Sorry EMS, but I actually just got another response from the matplotlib mailling list (Thanks goes out to Benjamin Root).

The code I am looking for is adjusting the savefig call to:

fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight')
#Note that the bbox_extra_artists must be an iterable

This is apparently similar to calling tight_layout, but instead you allow savefig to consider extra artists in the calculation. This did in fact resize the figure box as desired.

import matplotlib.pyplot as plt
import numpy as np

plt.gcf().clear()
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5,-0.1))
text = ax.text(-0.2,1.05, "Aribitrary text", transform=ax.transAxes)
ax.set_title("Trigonometry")
ax.grid('on')
fig.savefig('samplefigure', bbox_extra_artists=(lgd,text), bbox_inches='tight')

This produces:

[edit] The intent of this question was to completely avoid the use of arbitrary coordinate placements of arbitrary text as was the traditional solution to these problems. Despite this, numerous edits recently have insisted on putting these in, often in ways that led to the code raising an error. I have now fixed the issues and tidied the arbitrary text to show how these are also considered within the bbox_extra_artists algorithm.

这篇关于在轴外移动 matplotlib 图例使其被图形框截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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