Octave:将辅助 y 轴添加到现有绘图中

san*_*ica 6 matlab plot octave matlab-figure yaxis

我在 Win 10 下使用 Octave 4.2.1 便携式。

我在一张图表中有几个图,只有一个 y 轴,每个图都用一个plot(...)句子创建。我想在辅助 y 轴中添加一个图到此现有图,而不是从一开始就用plotyy, (编辑) 创建两个轴,并且能够正常工作,例如添加图例等。

正确的用法是什么?

如果我执行

plotyy(x, ysec) ;
Run Code Online (Sandbox Code Playgroud)

或者

ax = gca ;
plotyy(ax, x, ysec) ;
Run Code Online (Sandbox Code Playgroud)

我明白了

error: Invalid call to plotyy.  Correct usage is:    
 -- plotyy (X1, Y1, X2, Y2)
 -- plotyy (..., FUN)
 -- plotyy (..., FUN1, FUN2)
 -- plotyy (HAX, ...)
 -- [AX, H1, H2] = plotyy (...)
Run Code Online (Sandbox Code Playgroud)

显示了与 Matlab 类似的内容,但我不确定所有与使用 、 创建的辅助轴配合使用的代码plotyy也能与以这种方式创建的轴配合使用。

Cri*_*ngo 5

这里有两个选择。我已经在 MATLAB 中测试了这些,但我很确定它在 Octave 中也能以同样的方式工作。

让我们从一些正常绘制的随机数据开始:

% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));
Run Code Online (Sandbox Code Playgroud)

重新绘制图形,在第二个轴中添加新数据

在这里,我们从当前轴检索数据(当然,如果您保留制作第一个绘图时的轴句柄,那就更好了)。然后,我们使用plotyy包含旧数据和新数据的新图形绘制一个新图形。

ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata');    % Get data for line
y1 = get(h0,'ydata');
cla(ax)                  % Clear axes
plotyy(ax,x1,y1,x2,y2);  % Plot old and new data
Run Code Online (Sandbox Code Playgroud)

具有左轴和右轴的绘图

保留现有轴和绘图,使用新数据添加第二个轴

这里我们用它hold on来防止当前数据被删除,然后绘制新数据,同时plotyy在左轴上添加一个虚拟图(单个点 0,0)。然后我们删除这个虚拟图。

事实证明,添加这个虚拟图仍然会导致左轴发生变化。因此,此代码首先保留刻度线和限制的位置,然后在绘图后再次应用它们。它还使左轴的颜色与已有的线的颜色相同。

ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)
Run Code Online (Sandbox Code Playgroud)

具有左轴和右轴的绘图