要处理不同的缩放比例,您可以手动乘以left
中的值进行缩放,然后修改该边的刻度线。
% Automatically determine the scaling factor using the data itself
scale = max(right)/max(left);
% Create the left bar by scaling the magnitude
barh(1:numel(left), -left * scale, 'r');
hold on
barh(1:numel(right), right, 'b')
% Now alter the ticks.
xticks = get(gca, 'xtick')
% Get the current labels
labels = get(gca, 'xtickLabel');
if ischar(labels);
labels = cellstr(labels);
end
% Figure out which ones we need to change
toscale = xticks < 0;
% Replace the text for the ones < 0
labels(toscale) = arrayfun(@(x)sprintf('%0.2f', x), ...
abs(xticks(toscale)/scale), 'uniformoutput', false);
% Update the tick locations and the labels
set(gca, 'xtick', xticks, 'xticklabel', labels)
% Now add a different label for each side of the x axis
xmax = max(get(gca, 'xlim'));
label(1) = text(xmax/2, 0, 'Right Label');
label(2) = text(-xmax/ 2, 0, 'Left Label');
set(label, 'HorizontalAlignment', 'center', 'FontWeight', 'bold', 'FontSize', 15)
非常感谢,这看起来几乎等同于一个在例子!如果你不介意,我还有两个问题可以使它更加相似。由于我只使用正值,你会知道是否有两种方法获得正值:另外,您是否知道是否可以在x轴上添加两个单独的标签,每个数据集都有一个标签? –
@ Giovanni.R88更新 – Suever
太棒了!非常感谢! –