Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图例中的多行(具有不同的标记)_Python_Matplotlib - Fatal编程技术网

Python 图例中的多行(具有不同的标记)

Python 图例中的多行(具有不同的标记),python,matplotlib,Python,Matplotlib,我想在图例中显示具有相同标签但不同标记的多行 我要绘制的图形与此类似: 但是LineCollection中没有标记设置,我希望每一行都有不同的标记 有什么想法吗?多谢各位 您显示的图片源于。与这里所做的类似,您可以对图例处理程序进行子类化,以创建所选的图例 这里可以使用HandlerTuple,这样就可以直接提供图例中的完整行列表 import numpy as np import matplotlib.pyplot as plt from matplotlib.legend_handler

我想在图例中显示具有相同标签但不同标记的多行

我要绘制的图形与此类似:

但是
LineCollection
中没有标记设置,我希望每一行都有不同的标记


有什么想法吗?多谢各位

您显示的图片源于。与这里所做的类似,您可以对图例处理程序进行子类化,以创建所选的图例

这里可以使用HandlerTuple,这样就可以直接提供图例中的完整行列表

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple


class HandlerLinesVertical(HandlerTuple):
    def create_artists(self, legend, orig_handle,
                   xdescent, ydescent, width, height, fontsize,
                   trans):
        ndivide = len(orig_handle)
        a_list = []
        for i, handle in enumerate(orig_handle):
            y = (height / float(ndivide)) * i -ydescent
            line = plt.Line2D(np.array([0,1])*width, [-y,-y])
            line.update_from(handle)
            line.set_marker(None)
            point = plt.Line2D(np.array([.5])*width, [-y])
            point.update_from(handle)
            for artist in [line, point]:
                artist.set_transform(trans)
            a_list.extend([line,point])
        return a_list

x = np.linspace(0, 5, 15)

fig, ax = plt.subplots()

markers = ["o", "s", "d", "+", "*"]
lines = []
for i, marker in zip(range(5),markers):
    line, = ax.plot(x, np.sin(x) - .1 * i, marker=marker)
    lines.append(line)

ax.legend([tuple(lines)], ["legend entry"], handler_map={tuple:HandlerLinesVertical()},
           handleheight=8 )
plt.show()