xy打印python后matplotlib中的打印标签

xy打印python后matplotlib中的打印标签,python,matplotlib,graph,label,Python,Matplotlib,Graph,Label,我需要在坐标点的绘图函数之外创建线标签。我有一个列表要循环,以便为测试数据的每个阶段创建单独的行: fig = plt.figure(figsize=(11,8.5)) ax = fig.add_subplot(111) list_of_x_points = [[1,2,3],[4,5,6,7,8],[9,10,11,12],...] list_of_y_points = [[1,1,1],[2,2,2,2,2],[3,3,3,3],...] for i in range(le

我需要在坐标点的绘图函数之外创建线标签。我有一个列表要循环,以便为测试数据的每个阶段创建单独的行:

fig = plt.figure(figsize=(11,8.5))
ax = fig.add_subplot(111)    
list_of_x_points = [[1,2,3],[4,5,6,7,8],[9,10,11,12],...]
    list_of_y_points = [[1,1,1],[2,2,2,2,2],[3,3,3,3],...]

for i in range(len(list_of_x_points)):
    ax.plot(list_of_x_points[i], list_of_x_points[i], linecolor = "b" , linewidth = 1, linestyle = 'dashed'))
如果在
linestyle
之后插入标签,则每次循环时都会打印标签。我只想打印一次。我在for循环之后和之外尝试了
ax.legend(['Line Name'])
,但它不起作用。如何为图形中的线条指定标签?图表如下所示:


正如前面指出的,我创建了一个可测试版本的代码,
ax.legend()
工作正常

fig = plt.figure(figsize=(11,8.5))
ax = fig.add_subplot(111)    
list_of_x_points = [[1,2,3],[4,5,6,7,8],[9,10,11,12]]
list_of_y_points = [[1,1,1],[2,2,2,2,2],[3,3,3,3]]

for i in range(len(list_of_x_points)):
    ax.plot(list_of_x_points[i], list_of_x_points[i], "-b" , linewidth = 1, linestyle = 'dashed')
ax.legend(['Print this'])
plt.show()
我仍然不明白我的原始代码出了什么问题,我继续寻找,并找到了一个明显的答案:

for i in range(len(list_of_x_points)):
    ax.plot(list_of_x_points[i], list_of_x_points[i], "-b" , linewidth = 1, linestyle = 'dashed', label = "Print this" if i == 0 else "")
ax.legend()
plt.show()

我不知道这是否被认为是不好的做法,所以我想得到一些关于back的反馈,但它确实起了作用。

这里什么是
ax
。使其成为
ax。图例(['Line Name'])
如果您像之前所说的那样标记了数据(它没有反映在代码中,因此无法确定您实际做了什么。始终显示实际代码,不要描述它!)。
ax=fig.add\u子图(111)
我将编辑question@ImportanceOfBeingErnest您以前为其添加标签是什么意思?我想,以前是否在plot命令中插入标签并不重要。运行代码后,我可以看出,
ax.legend['Label name']
肯定可以工作。我收到的唯一错误消息是
AttributeError:Unknown property linecolor
,因此我建议不要这样做,这里只有
“b”
就足够了,因为plot需要一个格式字符串。也许这个错误也是您唯一的问题,所以构建图例的最后一个命令没有执行?