网站如何自己做优化,广州优壹网络科技有限公司,东莞市官网网站建设哪家好,外贸网站推广服务这个方法的核心#xff0c;是手动的获得图中的handlers和labels#xff0c;然后对它们进行切分和提取#xff0c;最后分为几个legend进行显示。代码如下#xff1a; 后来对下面的代码进行修改#xff0c;通过handlers, labels get_legend_handles_labels(axs[axis])自动的…这个方法的核心是手动的获得图中的handlers和labels然后对它们进行切分和提取最后分为几个legend进行显示。代码如下 后来对下面的代码进行修改通过handlers, labels get_legend_handles_labels(axs[axis])自动的获得handler。不再需要诸如handlers [ax.scatter(range(10), [i * x for x in range(10)], labelfLine {i}) for i in range(7)]之类的手动提取handler。源代码中这个依然进行了保留是因为这个代码还是一个绘图过程 # -*- coding: utf-8 -*-Time : 2024/1/5 20:55 \n
Author : 月司 \n
Email : 815603884qq.com \n
File : 图像图例的横向显示以及纵向显示.py \n
version : 1.0 \n
Software: PyCharm \n
Desc :
import matplotlib.pyplot as plt
import matplotlib.axes
from typing import List, Tuple, Any
#这个函数可以获得axis对象的已经绘制的artist中的所有的hander和labels这个可以只给它一个axis参数。注意这个参数需要是列表类的。
from matplotlib.legend import _get_legend_handles_labels as get_legend_handles_labelsdef custom_legend_layout(axis: matplotlib.axes.Axes,handlers: List[Any]None,labels: List[str]None,n_items: int 3,offset: float 0.05,vertical: bool False,loc: str upper right,first_bbox_to_anchor: Tuple[float, float] (1, 1),**kwargs) - None:A function to arrange legend items in a custom layout.:param axis: Axis object on which to place the legends.:param lines: List of line objects to include in the legends.:param labels: List of labels corresponding to the line objects.:param n_items: Number of items per row (if verticalFalse) or column (if verticalTrue).:param offset: Vertical offset between rows (or horizontal offset between columns if verticalTrue).:param vertical: If True, legends are arranged vertically, otherwise horizontally.:param loc: Location anchor for all legends.:param first_bbox_to_anchor: :param first_bbox_to_anchor: ~matplotlib.transforms.BboxBase instance, Bbox anchor of the first legend.Bbox anchor of the first legend.:param kwargs: Additional keyword arguments to pass to the legend function.if (handlers is None) ! (labels is None): # Check if only one of handlers or labels is providedraise ValueError(Both handlers and labels must be specified if one is provided.)if (handlers is None) and (labels is None): # get default handlers and labels from axhandlers,labelsget_legend_handles_labels(axs[axis]) # note: the param axs is list object# 确保n_items不为0避免除以0的错误n_items max(1, n_items)# 计算需要多少个图例n_legends len(handlers) // n_items (1 if len(handlers) % n_items else 0)# 计算每个图例的bbox_to_anchorfor i in range(n_legends):start_idx i * n_itemsend_idx min(start_idx n_items, len(handlers))legend_lines handlers[start_idx:end_idx]legend_labels labels[start_idx:end_idx]if vertical:# 对于垂直布局ncol 1if i 0:bbox_anchor first_bbox_to_anchorelse:# 计算后续图例的bbox_to_anchorbbox_anchor (first_bbox_to_anchor[0] i * offset, first_bbox_to_anchor[1])else:# 对于水平布局ncol len(legend_lines)if i 0:bbox_anchor first_bbox_to_anchorelse:# 计算后续图例的bbox_to_anchorbbox_anchor (first_bbox_to_anchor[0], first_bbox_to_anchor[1] - i * offset)legend axis.legend(legend_lines, legend_labels, locloc, bbox_to_anchorbbox_anchor, ncolncol, frameonFalse, **kwargs)axis.add_artist(legend)if __name__ __main__:# 示例使用这个函数fig, ax plt.subplots()handlers [ax.scatter(range(10), [i * x for x in range(10)], labelfLine {i}) for i in range(7)]# 调用函数横向排列图例custom_legend_layout(ax, n_items3, offset0.25, verticalTrue,locupper center, first_bbox_to_anchor(0.2, 0.8))from matplotlib.legend import _get_legend_handles_labels as get_legend_handles_labelshandles,labelsget_legend_handles_labels([ax])plt.show() 注意handlers, labels get_legend_handles_labels(axs[axis])可以自动的获得handler和label。 下面是使用这个函数的几个示例 在Matplotlib中bbox_to_anchor 参数用于指定图例legend的位置。这个参数接受一个元组通常包含两个元素分别代表图例在X轴和Y轴上的位置。这些位置的值通常是基于图表的坐标系统可以是
轴坐标axis coordinates图表坐标matplotlib.transforms.BboxBase图坐标figure coordinates或其他坐标系统。 图表坐标和图坐标都是相对坐标系 在前面的代码示例中bbox_to_anchor 的值例如 (0.5, 1)是基于图表坐标系统的。图表坐标系统中(0, 0) 代表图表的左下角而 (1, 1) 代表图表的右上角。因此一个元组 (0.5, 1) 表示图例位于图表的顶部中心。 另外如果是这个图片的左下角是00那么其实是figure的坐标系统 至于 offset 参数在示例中为 0.05它用于在指定方向上对图例的位置进行微调。在水平布局verticalFalse的情况下offset 用于垂直方向的偏移在垂直布局verticalTrue的情况下offset 用于水平方向的偏移。
例如如果 first_bbox_to_anchor 是 (0.5, 1)顶部中心并且 offset 是 0.05那么第二个图例的 bbox_to_anchor 将会是 (0.5, 1 - 0.05)即稍微向下偏移。每个后续的图例都会继续这样向下偏移。
需要注意的是offset 的值 0.05 也是图表坐标值。这意味着它的实际偏移量取决于图表的大小。在不同大小的图表中相同的 offset 值会产生不同的实际偏移效果。这个值可能需要根据具体的图表尺寸和所需的视觉效果进行调整。