Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

plotyy

(不推荐)创建具有两个 y 轴的图形

    不推荐使用 plotyy请改用 yyaxis有关更新代码的详细信息,请参阅版本历史记录

    说明

    示例

    plotyy(X1,Y1,X2,Y2) 绘制 Y1X1 的图,在左侧显示 y 轴标签,并同时绘制 Y2X2 的图,在右侧显示 y 轴标签。

    plotyy(X1,Y1,X2,Y2,f) 使用指定的绘图函数来绘制数据。

    示例

    plotyy(X1,Y1,X2,Y2,f1,f2) 使用绘图函数 f1 绘制 X1Y1 的图,使用绘图函数 f2 绘制 X2Y2 的图。

    plotyy(ax,___) 在指定坐标区(而不是当前坐标区)中显示左侧图。如果 ax 是一个向量,则 plotyy 使用向量中的第一个坐标区对象。将坐标区指定为上述任一语法中的第一个参量。

    示例

    [a,p1,p2] = plotyy(___) 返回 a 中的两个坐标区对象以及 p1p2 中的两个图形对象。a(1) 是左坐标区,而 a(2) 是右坐标区。创建坐标区和图后,使用 ap1p2 来修改其属性。

    示例

    全部折叠

    使用两个 y 轴在一个图上绘制两个数据集。

    x = 0:0.01:20;
    y1 = 200*exp(-0.05*x).*sin(x);
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    
    figure
    plotyy(x,y1,x,y2)

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

    使用两个 y 轴在一个图上绘制两个数据集。添加标题和轴标签。

    x = 0:0.01:20;
    y1 = 200*exp(-0.05*x).*sin(x);
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    
    figure
    [hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
    
    title("Multiple Decay Rates")
    xlabel("Time (\musec)")
    
    ylabel(hAx(1),"Slow Decay") % left y-axis 
    ylabel(hAx(2),"Fast Decay") % right y-axis

    Figure contains 2 axes objects. Axes object 1 with title Multiple Decay Rates, xlabel Time (\musec), ylabel Slow Decay contains an object of type line. Axes object 2 with ylabel Fast Decay contains an object of type line.

    使用两个 y 轴在一个图上绘制两个数据集。更改线型。

    x = 0:0.01:20;
    y1 = 200*exp(-0.05*x).*sin(x);
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    
    [hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
    hLine1.LineStyle = "--";
    hLine2.LineStyle = ":";

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

    使用两个 y 轴在一个图上绘制两个数据集。对与左 y 轴关联的数据使用线图。对与右 y 轴关联的数据使用针状图。

    x = 0:0.1:10;
    y1 = 200*exp(-0.05*x).*sin(x);
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    
    figure 
    plotyy(x,y1,x,y2,@plot,@stem)

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type stem.

    使用两个 y 轴在一个图上绘制三个数据集。绘制一个与左 y 轴关联的数据集。使用包含两列的矩阵绘制与右 y 轴关联的两个数据集。

    x = linspace(0,10);
    y1 = 200*exp(-0.05*x).*sin(x);
    y2 = 0.8*exp(-0.5*x).*sin(10*x);
    y3 = 0.2*exp(-0.5*x).*sin(10*x);
    
    figure
    [hAx,hLine1,hLine2] = plotyy(x,y1,[x',x'],[y2',y3']);

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains 2 objects of type line.

    输入参数

    全部折叠

    x 坐标,指定为标量、向量或矩阵。

    数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

    y 坐标,指定为标量、向量或矩阵。

    数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

    用于绘制数据的绘图函数,指定为函数句柄或作为绘图函数名称的字符向量,例如,semilogxsemilogyloglogstem,或任何接受语法 h = function(x,y) 的 MATLAB® 函数。

    示例: plotyy(x1,y1,x2,y2,@loglog)

    示例: plotyy(x1,y1,x2,y2,'loglog')

    目标坐标区,指定为 Axes 对象。如果未指定坐标区,则 MATLAB 将第一组数据绘制到当前坐标区中,或创建一个 Axes 对象(如果不存在)。

    输出参量

    全部折叠

    两个坐标区,以两个 Axes 对象数组形式返回。a(1) 是左坐标区,而 a(2) 是右坐标区。使用 a 修改坐标区的属性。

    图形对象。创建绘图后,使用 p 修改该绘图的属性。

    扩展功能

    版本历史记录

    在 R2006a 之前推出

    全部折叠

    R2016a: 不推荐

    不推荐使用 plotyy。请改用 yyaxis。目前没有删除 plotyy 的计划。

    从 R2016a 开始,可以使用 yyaxis 函数创建具有两个 y 轴的图。yyaxis 函数相对于 plotyy 函数有多项优势。

    • plotyy 不同,yyaxis 函数会创建一个具有两个 y 轴的 Axes 对象。plotyy 创建两个可能不同步的重叠 Axes 对象。

    • 您可以将 yyaxis 与任何二维绘图函数结合使用。plotyy 仅限于与 function(x,y) 形式的绘图函数结合使用。它不适用于其他绘图函数,例如 errorbar

    下表显示了 plotyy 的一些典型用法,以及如何更新代码以改用 yyaxis

    不推荐推荐
    plotyy(x1,y1,x2,y2)
    yyaxis left 
    plot(x1,y1) 
    yyaxis right 
    plot(x2,y2)
    plotyy(x1,y1,x2,y2, ...
    'function1','function2')
    yyaxis left 
    function1(x1,y1) 
    yyaxis right 
    function2(x2,y2)