2 回答
TA贡献1951条经验 获得超3个赞
图例与边界框边缘的距离由borderaxespad参数设置。的borderaxespad是在字体大小的倍数为单位-使它自动独立的轴尺寸。所以在这种情况下,
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.random.randn(5)
fig, ax = plt.subplots(constrained_layout=True)
ax.plot(x, y, label='data1')
ax.plot(x, y-1, label='data2')
legend = ax.legend(loc="upper center", bbox_to_anchor=(0.5,0), borderaxespad=2)
plt.show()
在轴图形底部的地方标题中提出了一个类似的问题,即在轴下方以恒定距离显示标题?
TA贡献1871条经验 获得超13个赞
您可以使用画布的调整大小事件来更新bbox_to_anchor每次更新时的值。要计算新值,您可以使用轴变换 ( Bbox.inverse_transformed(ax.transAxes))的逆变换,它将以像素为单位的屏幕坐标转换为 .x 中通常使用的轴坐标bbox_to_anchor。
这是一个支持将图例放在轴的所有四个边上的示例:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.transforms import Bbox
class FixedOutsideLegend:
"""A legend placed at a fixed offset (in pixels) from the axes."""
def __init__(self, ax, location, pixel_offset, **kwargs):
self._pixel_offset = pixel_offset
self.location = location
if location == 'right':
self._loc = 'center left'
elif location == 'left':
self._loc = 'center right'
elif location == 'upper':
self._loc = 'lower center'
elif location == 'lower':
self._loc = 'upper center'
else:
raise ValueError('Unknown location: {}'.format(location))
self.legend = ax.legend(
loc=self._loc, bbox_to_anchor=self._get_bbox_to_anchor(), **kwargs)
ax.figure.canvas.mpl_connect('resize_event', self.on_resize)
def on_resize(self, event):
self.legend.set_bbox_to_anchor(self._get_bbox_to_anchor())
def _get_bbox_to_anchor(self):
"""
Find the lengths in axes units that correspond to the specified
pixel_offset.
"""
screen_bbox = Bbox.from_bounds(
0, 0, self._pixel_offset, self._pixel_offset)
try:
ax_bbox = screen_bbox.inverse_transformed(ax.transAxes)
except np.linagl.LinAlgError:
ax_width = 0
ax_height = 0
else:
ax_width = ax_bbox.width
ax_height = ax_bbox.height
if self.location == 'right':
return (1 + ax_width, 0.5)
elif self.location == 'left':
return (-ax_width, 0.5)
elif self.location == 'upper':
return (0.5, 1 + ax_height)
elif self.location == 'lower':
return (0.5, -ax_height)
x = np.arange(5)
y = np.random.randn(5)
fig, ax = plt.subplots(tight_layout=True)
ax.plot(x, y, label='data1')
ax.plot(x, y-1, label='data2')
legend = FixedOutsideLegend(ax, 'lower', 20, ncol=2)
plt.show()
结果:
添加回答
举报