繁体   English   中英

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

[英]How to add legend elements in Matlab in the plot itself

我想以某种方式在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');

我认为您可能想使用text对象而不是图例。 这是一个示例(请注意,因为我没有Financial Toolbox ,所以不得不使用datetick而不是dateaxis ):

% 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');

和结果情节:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM