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

Creating a 2 line graph on left axis and a bar graph on right axis 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")

虽然 plotyy 使用不同的左右 yaxes 绘制两个 正常图 的命令,但我不认为你可以用它做你想做的事。 (即,将两个图与 条形图 混合)。然而,plotyy 所做的实际上是一个简单的例子,即在同一位置叠加两个轴并制作一个 'see-through'。因此,您通常可以使用相同的方法。

这是为实现此目的而重新设计的上面的示例(加上一些额外的亮点):

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);