如何使用 MATLAB 中的线或注释对象绘制轴外的对象?

How can I plot objects outside of an axes using Line or annotation objects in MATLAB?

我想在 MATLAB 图中轴外的每个数据点创建一条带有圆形标记的线,类似于

line([x1 x2],[y1 y2],'Color','k','Marker','o')

会产生。

要将线对象放置在轴外,我只是尝试使用 annotation:

annotation('line',phi1([x1 x2]),phi2([y1 y2]),'Color','k','Marker','o')

其中 phi1phi2 是适合坐标 xy 在当前图形轴内的适当坐标变换。

我希望它能工作,因为线对象有一个 marker 属性。但是,对于 annotation,我收到以下错误消息:

Error using matlab.graphics.shape.Line/set
There is no Marker property on the Line class.

Error in matlab.graphics.chart.internal.ctorHelper (line 8)
    set(obj, pvpairs{:});

Error in matlab.graphics.shape.Line

Error in annotation (line 128)
        h = matlab.graphics.shape.Line(aargs{:});

同样,用annotation函数绘制矩形时,不能设置Curvature属性。 annotation 似乎不支持这些类型的属性,即使它像 linerectangle 函数一样创建了一个直线或矩形对象。我试着摆弄注释句柄和子句,但没有成功。

有什么解决方法吗?

different types of annotation objects are a separate set of class types than the usual line or rectangle 个对象,支持一组精简的属性。例如,典型的 line 对象是 class 类型 matlab.graphics.primitive.Line,但注释线对象是 class 类型 matlab.graphics.shape.Line.

而不是使用 linerectangle 对象的 annotation objects, you can simply set the 'Clipping' property'off' 以允许它们在轴限制之外单独渲染。例如,这段代码:

hAxes = subplot(1, 2, 1);
axis(hAxes, [0 1 0 1]);    % Freeze axis limits
hLine = line([0.5 2], [0.5 0.5], 'Color', 'k', ...
                                 'Marker', 'o', ...
                                 'Clipping', 'off');
hRect = rectangle(hAxes, 'Position', [1.5 0.1 1 0.25], ...
                         'Curvature', [0.2 0.2], ...
                         'FaceColor', 'r', ...
                         'Clipping', 'off');

产生这个情节:

或者,您可以通过将 'Clipping' property 旋转到 所有 轴对象关闭裁剪'off'.