MATLAB:如何绘制具有不同比例和不同数据集的多个水平条形图?

Gio*_*R88 5 matlab plot bar-chart matlab-figure

我想绘制一个完全如下图所示的条形图.

在此输入图像描述

我无法做的是绘制2组数据,一组分别位于"0"的左侧,另一组分别位于右侧,每个数据在x轴上使用不同的比例.使用该函数barh有关于如何移动基线的说明,但在这种情况下,有两组具有不同比例的不同数据.

例如,我试图绘制以下数组:

left = [.1; .5; .4; .6; .3]; % Scale 0-1, grows leftwards
right = [24; 17; 41; 25; 34]; % Scale 0-35, grows rightwards
Run Code Online (Sandbox Code Playgroud)

有什么提示吗?

Sue*_*ver 4

要处理不同的缩放,您可以手动乘以要缩放的值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) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述