在 Python 中读取图例的高度 [英] Read height of legend in Python

查看:25
本文介绍了在 Python 中读取图例的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有很多信息和线条的图,所以有时我倾向于使用bbox_to_anchor 将图例放在图本身之外.我也更喜欢有一个情节的标题,但是在这种情况下,它的位置会与图例一致.下面的示例只是问题的说明.

I have some plots with a lot of information and lines, so sometimes I tend to put the legend outside the plot itself using bbox_to_anchor. I also prefer to have a title of the plot, but this will positionally coincide with the legend in that case. The following example below is just an illustration of the problem.

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
r = 1 + np.sin(4 * np.pi * t)
q = 1 + np.sin(6 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s, label='S')
ax.plot(t, r, label='R')
ax.plot(t, q, label='Q')
leg = ax.legend(loc=3, ncol=3, bbox_to_anchor=(.0, 1.02, 1., .102), borderaxespad=0., mode='expand')
ax.set_title('SIMPLE PLOT', y=1.1)
plt.show()

为了避免这种情况,我设置了某种 y 值(例如 y=1.1).我想自动执行此过程,因为我不断使用新数据更新同一地块,因此图例的大小会增加,因此需要相应地调整标题的位置.

To avoid this, I set some kind of y-value (e.g. y=1.1). I would like to automate this process because I keep updating the same plot with new data, so the legend grows in size, and I need to adjust the position of the title accordingly.

  • 有没有办法使这个过程自动化?
  • Python 中是否有一个函数可以读取图例的高度,以便可以使用它来调整标题位置?

推荐答案

图例的高度在绘制时确定.您可以通过 legend.get_window_extent()绘制图形后得到它.生成的边界框以像素为单位.为了找到标题的偏移量,您需要从轴的上限中减去图例的上限.因此,您还需要获取以像素为单位的轴位置.

The height of the legend is determined at draw time. You can get it after having drawn the figure via legend.get_window_extent(). The resulting bounding box is in units of pixels. In order to find the offset of the title, you will need to subtract the upper limit of the legend from the upper limit of the axes. So you need to get the axes position in pixels as well.

标题可以在图形坐标 (y=1.1) 或点 (pad=20) 中偏移.我建议在这里使用点,使其独立于轴的大小.因此,您可以计算较高位置的差异,将像素转换为点(即距离[像素] * ppi/dpi ),并在点中添加一些恒定的偏移量(因为通常您不希望标题正好位于图例的边界上).然后将该数字用作 pad.

The title can be offset either in figure coordinates (y=1.1) or points (pad=20). I would suggest to use points here, to make it independent of the size of the axes. So you can calculate the difference in upper positions, convert from pixels to points (i.e. distance [pixels] * ppi / dpi) and add some constant offset in points (because usually you would not want the title to sit exactly on the border of the legend). Then use that number as pad.

import numpy as np
import matplotlib.pyplot as plt


fig, ax = plt.subplots(constrained_layout=True)

ax.plot([1,2,3], np.random.rand(3,5), label='Label')

leg = ax.legend(loc="lower center", ncol=3, bbox_to_anchor=(.0, 1.02, 1., 1.02),
                borderaxespad=0, mode='expand')

fig.canvas.draw()
leg_box = leg.get_window_extent()
ax_box = ax.get_position().transformed(fig.transFigure)
pad = (leg_box.y1 - ax_box.y1)*72./fig.dpi + 6
ax.set_title('SIMPLE PLOT', pad=pad)

plt.show()

请注意,这里我还使用了 constrained_layout 来使标题不受图形边界的限制.

Note that here I also used constrained_layout to have the title not cropped by the figure boundaries.

这篇关于在 Python 中读取图例的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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