python - 图例中的多行(具有不同标记)
解决方案
您展示的图片源于图例演示。与那里所做的类似,您可以将图例处理程序子类化以创建选择的图例。
这里可以使用 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()
推荐阅读
- linux - 如何在awk中将文件的列名作为变量传递
- python-3.x - 使用 Python 逐行写入 CSV
- javascript - 如何合并 2 个图像(一个有形状)并使其在 Canvas 中透明
- c# - 将SVG添加到PDF时iText7设置文本大小
- gitolite - 警告:无法执行“git”:/home/git/bin/lib/Gitolite/Common.pm 第 152 行没有这样的文件或目录
- java - 将值从 Activity 传递给适配器
- python - 如何从 python 中睡眠我的计算机?
- c++ - 定义 HAS_XXX / NO_XXX 以支持/不支持某个功能
- keras - 自定义层导致“tensorflow.python.framework.errors_impl.InvalidArgumentError:不兼容的形状:[128] vs. [128,256,256]”
- c# - 从 Excel 获取数据仅显示最后一行