嗯,有几种方法可以在同一个图中显示多个数据系列。
我将使用一个小示例数据集以及相应的颜色:
%% Data
t = 0:100;
f1 = 0.3;
f2 = 0.07;
u1 = sin(f1*t); cu1 = 'r'; %red
u2 = cos(f2*t); cu2 = 'b'; %blue
v1 = 5*u1.^2; cv1 = 'm'; %magenta
v2 = 5*u2.^2; cv2 = 'c'; %cyan
首先,当您希望所有东西都在同一轴上时,您将需要hold https://nl.mathworks.com/help/techdoc/ref/hold.html功能:
%% Method 1 (hold on)
figure;
plot(t, u1, 'Color', cu1, 'DisplayName', 'u1'); hold on;
plot(t, u2, 'Color', cu2, 'DisplayName', 'u2');
plot(t, v1, 'Color', cv1, 'DisplayName', 'v1');
plot(t, v2, 'Color', cv2, 'DisplayName', 'v2'); hold off;
xlabel('Time t [s]');
ylabel('u [some unit] and v [some unit^2]');
legend('show');
您会发现这在许多情况下是正确的,但是,当两个量的动态范围相差很大时(例如,u
值小于 1,而v
值要大得多)。
其次,当你的数据很多或者数量不同时,也可以使用subplot https://nl.mathworks.com/help/techdoc/ref/subplot.html有不同的轴。我也用过这个功能linkaxes https://nl.mathworks.com/help/techdoc/ref/linkaxes.html链接 x 方向上的轴。当您在 MATLAB 中放大其中一个时,另一个将显示相同的 x 范围,这样可以更轻松地检查更大的数据集。
%% Method 2 (subplots)
figure;
h(1) = subplot(2,1,1); % upper plot
plot(t, u1, 'Color', cu1, 'DisplayName', 'u1'); hold on;
plot(t, u2, 'Color', cu2, 'DisplayName', 'u2'); hold off;
xlabel('Time t [s]');
ylabel('u [some unit]');
legend(gca,'show');
h(2) = subplot(2,1,2); % lower plot
plot(t, v1, 'Color', cv1, 'DisplayName', 'v1'); hold on;
plot(t, v2, 'Color', cv2, 'DisplayName', 'v2'); hold off;
xlabel('Time t [s]');
ylabel('v [some unit^2]');
legend('show');
linkaxes(h,'x'); % link the axes in x direction (just for convenience)
子图确实浪费了一些空间,但它们允许将一些数据保留在一起,而不会过度填充图。
最后,作为一个更复杂的方法的示例,使用以下方法在同一个图形上绘制不同的数量:plotyy https://nl.mathworks.com/help/techdoc/ref/plotyy.html函数(或者更好的是:yyaxis https://nl.mathworks.com/help/matlab/ref/yyaxis.html自 R2016a 起运行)
%% Method 3 (plotyy)
figure;
[ax, h1, h2] = plotyy(t,u1,t,v1);
set(h1, 'Color', cu1, 'DisplayName', 'u1');
set(h2, 'Color', cv1, 'DisplayName', 'v1');
hold(ax(1),'on');
hold(ax(2),'on');
plot(ax(1), t, u2, 'Color', cu2, 'DisplayName', 'u2');
plot(ax(2), t, v2, 'Color', cv2, 'DisplayName', 'v2');
xlabel('Time t [s]');
ylabel(ax(1),'u [some unit]');
ylabel(ax(2),'v [some unit^2]');
legend('show');
这看起来确实很拥挤,但当信号的动态范围差异很大时,它会派上用场。
当然,没有什么可以阻止您结合使用这些技术:hold on
和...一起plotyy
and subplot
.
edit:
For quiver https://nl.mathworks.com/help/techdoc/ref/quiver.html,我很少使用该命令,但无论如何,你很幸运,我不久前编写了一些代码以方便绘制矢量场图。您可以使用与上面解释的相同的技术。我的代码远非严格,但这里是:
function [u,v] = plotode(func,x,t,style)
% [u,v] = PLOTODE(func,x,t,[style])
% plots the slope lines ODE defined in func(x,t)
% for the vectors x and t
% An optional plot style can be given (default is '.b')
if nargin < 4
style = '.b';
end;
% http://ncampbellmth212s09.wordpress.com/2009/02/09/first-block/
[t,x] = meshgrid(t,x);
v = func(x,t);
u = ones(size(v));
dw = sqrt(v.^2 + u.^2);
quiver(t,x,u./dw,v./dw,0.5,style);
xlabel('t'); ylabel('x');
当调用时:
logistic = @(x,t)(x.* ( 1-x )); % xdot = f(x,t)
t0 = linspace(0,10,20);
x0 = linspace(0,2,11);
plotode(@logistic,x0,t0,'r');
this yields:
如果您需要更多指导,我发现我的来源中的那个链接 http://ncampbellmth212s09.wordpress.com/2009/02/09/first-block/非常有用(尽管格式错误)。
另外,你可能想看看 MATLAB 帮助,它真的很棒。只需输入help quiver
or doc quiver
进入 MATLAB 或使用我上面提供的链接(这些链接应提供与doc
).