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

标签 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 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对象而不是图例。这是一个示例(请注意,我必须使用 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');

结果图:

enter image description here

关于matlab - 如何在绘图本身的 Matlab 中添加图例元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732312/

相关文章:

matlab - 'varargin' 函数在仅提供名称-值对时给出错误 : MATLAB

Matplotlib 和时间戳造成麻烦

使用 Gnuplot 绘制红外光谱

algorithm - 在分解过程中选择最频繁对象的算法

python - 使用 system() 从 MATLAB 调用 Python : issue importing non-standard Python libraries

python - Matlab 中的高效字符串连接

matlab - MATLAB 中隐式函数 f(x,y,z)=0 的轮廓

python - 使用 matplotlib 的大量子图

algorithm - 设计一种算法,在线性时间内找到该图的最小生成树

python - 随机遍历有向图