【问题标题】:How to add legend elements in Matlab in the plot itself如何在绘图本身中添加 Matlab 中的图例元素
【发布时间】:2019-05-12 22:13:19
【问题描述】:

我想在 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');

【问题讨论】:

    标签: matlab plot graph legend legend-properties


    【解决方案1】:

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

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

    以及由此产生的情节:

    【讨论】:

    • 非常感谢,就这样!
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 2012-06-30
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多