如何在绘图本身中添加Matlab中的图例元素

如何在绘图本身中添加Matlab中的图例元素,matlab,plot,graph,legend,legend-properties,Matlab,Plot,Graph,Legend,Legend Properties,我想在Matlab中以某种方式标记垂直线。我可以想象两种选择:要么让图例条目紧挨着每条垂直线本身,要么让垂直线在图形中编号,然后让数字重新出现在图例中。这两种可能吗 我不想使用不同的颜色或图形模式,因为我有几条垂直线,图形很难阅读 x是日期数字的向量,y是价格数据。Date1和Date2是x元素的日期 plot(x,y), grid on; dateaxis('x',17); line([Date1 Date1], ylim); % I would like to have a legend e

我想在Matlab中以某种方式标记垂直线。我可以想象两种选择:要么让图例条目紧挨着每条垂直线本身,要么让垂直线在图形中编号,然后让数字重新出现在图例中。这两种可能吗

我不想使用不同的颜色或图形模式,因为我有几条垂直线,图形很难阅读

x是日期数字的向量,y是价格数据。Date1和Date2是x元素的日期

plot(x,y), grid on;
dateaxis('x',17);
line([Date1 Date1], ylim); % I would like to have a legend entry for this right at the line in the graph
line([Date2 Date2], ylim); % I would like to have a legend entry for this right at the line in the graph
legend('Price');

我想你可能想使用对象而不是图例。下面是一个示例(注意,我必须使用,而不是因为我没有以下选项):

结果是:


非常感谢,就这样!
% Some sample data:
x = datenum(now():(now()+days(6)));
y = 1:7;

% Plot data:
plot(x, y);
grid on;
datetick('x');

% Make horizontal red lines:
line([x(1) x(1)], ylim, 'Color', 'r');
line([x(end) x(end)], ylim, 'Color', 'r');

% Add text:
text(x(1), mean(ylim), ' left');
text(x(end), mean(ylim), 'right ', 'HorizontalAlignment', 'right');