在左轴上创建2线图,在右轴上创建条形图Octave

时间:2017-06-14 13:28:27

标签: octave

我难以在Octave中创建2 y轴图。目前我可以制作2线图。但是,我还没能找到能帮助解决问题的功能。我已经尝试使用plotyy函数,但我不确定你是否可以使用这个函数有两个左侧轴线图和一个右侧图。这是我到目前为止在我的尝试中编写的代码。

labels = ["Data 1"; "Data 2"; "Data 3"; "Data 4"; "Data 5"]
y1 = [137, 15, 2, 3, 37]
y2 = [43, 1, 67, 97, 41]
x = [1, 2, 3, 4, 5]
y3 = [0, .2, .3, .104, .09]
z1 = plot(x, y1, y2) 
plot(x, y1, y2)
hold on
plot(x, y2)
xlabel("Version")
yyaxis left
ylabel("Y axis")
set(gca,'XTickLabel',labels)
yyaxis right
z = bar(x,y3)
z
yyaxis right
ylabel("Data")

1 个答案:

答案 0 :(得分:0)

虽然plotyy 使用不同的左右yaxes绘制两个普通图的命令,但我不认为你可以做你做的事正试图用它做。 (即将两个图表与条形图混合)。然而,plotyy所做的实际上是一个简单的例子,它将两个轴重叠在同一个位置并制作一个透视图。所以你可以使用相同的方法。

以上您的例子重新实现了这一目标(加上一些额外的金光闪闪):

x  = [1, 2, 3, 4, 5]; labels = ["Data 1"; "Data 2"; "Data 3"; "Data 4"; "Data 5"];
y1 = [137, 15, 2, 3, 37]; y2 = [43, 1, 67, 97, 41]; y3 = [0, .2, .3, .104, .09];

ax_left = axes('position', [0.15, 0.12, 0.7, 0.82]); % manually positioned
plot (x, y1, 'r-', 'linewidth', 3); hold on
plot (x, y2, 'g:', 'linewidth', 3); hold off
set (ax_left, ...
     'fontsize', 16, 'fontweight', 'bold', 'labelfontsizemultiplier', 1.2, ...
     'color', 'none', ...         % makes 'background' see-through
     'box', 'off', ...            % prevents left axis ticks in the right axis
     'xlim', [0, 6], 'ylim', [0, 150], 'xtick', x, 'xticklabel', labels);
xlabel('Version'); ylabel('Left Y-Axis');

ax_right = axes('position', [0.15, 0.12, 0.7, 0.82]); % same position as above
bar (x, y3, 0.5, 'facecolor', [0, 0.5, 1]);  % nice narrow light blue columns
set (ax_right, ...
     'fontsize', 16, 'fontweight', 'bold',  'labelfontsizemultiplier', 1.2, ...
     'yaxislocation', 'right', ...
     'ycolor', [0, 0.5, 1], ... % same nice light blue color as bar chart
     'box', 'off', 'xlim', [0, 6], 'ylim', [0, 0.35], 'xtick', []);
ylabel('Data');

% ensure axes are stacked in the order you want them to appear (bottom to top)
axes(ax_right);
axes(ax_left);

enter image description here