要處理不同的縮放比例,您可以手動乘以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
太棒了!非常感謝! –