两个y轴绘制Matlab中的多个数据集

时间:2014-12-23 12:14:44

标签: matlab plot

我正在尝试在Matlab中创建两个y轴图。 我有两组数据,每组有三个相似类型的图。当我试图绘制它时,右侧y轴上的缩放变得混乱。我将不胜感激。

X = [50, 100, 200, 400];

YSKL_Temporal_WOFAE    = [3.2000       2.3354      1.9428          1.7658];
YSKL_Spatial_WOFAE     = [0.9225       0.9724      1.0986          1.1770];
YSKL_Spatial_WithFAE   = [0.2635       0.1653      0.1513           0.1618];

YMSRE_Temporal_WOFAE    = [0.3559       0.3027    0.2733          0.2636];
YMSRE_Spatial_WOFAE     = [.3151       .2689      .2551           0.2524];
YMSRE_Spatial_WithFAE   = [.0933       .0648      0.0663          0.0640];

figure(1);
[AX, p1, p2] = plotyy(X, YSKL_Temporal_WOFAE, X, YMSRE_Temporal_WOFAE);
set(AX,'XTick', X);   % This fixes X-axis tick mark (same as data axis)

set(get(AX(1),'Ylabel'),'String','SKL Score') 
set(get(AX(2),'Ylabel'),'String','Norm. Residuals') 
xlabel('Time (\musec)') 
title('SKL and Norm. Residual plotted on different y-axes') 
set(p1,'LineStyle','--')
set(p2,'LineStyle',':')

axes(AX(1))
hold on
plot(X, YSKL_Spatial_WOFAE);
hold on
plot(X, YSKL_Spatial_WithFAE);
ylim([0 4])
hold off

axes(AX(2))
hold on
plot(X, YMSRE_Spatial_WOFAE);
hold on
plot(X, YMSRE_Spatial_WithFAE);
ylim([0.0 0.4])
hold off

Plot看起来像这样: enter image description here

请注意右y轴上的比例

此致 Dushyant

1 个答案:

答案 0 :(得分:2)

在调用plotyy后,第二个(右侧)y轴刻度标签被“冻结”。调整ylim上的AX(2)时,这对标签没有影响 因此,axes appearance必须重置为auto 使用以下代码和问题中的示例:

axes(AX(2))
hold on
plot(X, YMSRE_Spatial_WOFAE);
hold on
plot(X, YMSRE_Spatial_WithFAE);
ylim([0.0 0.4])
% include the following statement to allow 
% the second y-axis to reset the ticks:
set(AX(2), 'YTickMode', 'auto', 'YTickLabelMode', 'auto')
hold off

将产生这个情节:

enter image description here