将条形图和反向条形绘制到Y轴

时间:2017-08-30 16:42:15

标签: matlab plot bar-chart

我试图使用相同的y轴绘制两个条形序列。其中一个序列位于底部,另一个序列位于顶部(从上到下),y轴分为两个(一半用于底部条形,一半用于顶部条形)。底栏使用一种颜色,如绿色,顶部栏使用另一种颜色,红色,y轴使用轴上每一半的相应颜色。

实施例: enter image description here

问题:如何按照上图所示的方式拆分y轴?

感谢您的帮助! = d

1 个答案:

答案 0 :(得分:7)

您可以通过分层三个轴并相应地调整their properties来创建此效果。例如:

% The larger left axes:
hLeft = axes('Position', [0.1 0.1 0.8 0.8], ...  % Normalized position
             'XLim', [0 11], 'YLim', [0 3], ...  % Set desired limits
             'Box', 'off');                      % Turn off outline
hLeft.XLabel.String = 'Time';         % Add an x label
hLeft.YLabel.String = 'Line';         % Add a y label
hLeft.Title.String = 'Bar-bar-plot';  % Add a title
hLine = line(hLeft, 1:10, rand(1,10)+1, 'Color', 'k');  % Plot a sample line

% The lower right axes:
hLower = axes('Position', [0.1 0.1 0.8 0.4], ...         % Position over bottom half
              'XLim', [0 11], 'YLim', [0 1.5], ...       % Set desired limits
              'YColor', [0 0.5 0], 'Color', 'none', ...  % Change colors
              'YAxisLocation', 'right');                 % Position y axis
hLower.XAxis.Visible = 'off';    % Turn of x axis
hLower.YLabel.String = 'Bar 1';  % Add a y label
hold on;
hBarLower = bar(hLower, 1:10, rand(1,10), ...  % A sample bar plot
                'FaceColor', 'none', 'EdgeColor', [0 0.5 0]);

% The upper right axes:
hUpper = axes('Position', [0.1 0.5 0.8 0.4], ...       % Position over top half
              'XLim', [0 11], 'YLim', [0 1.5], ...     % Set desired limits
              'YColor', [1 0 0], 'Color', 'none', ...  % Change colors
              'YAxisLocation', 'right', ...            % Position y axis
              'YDir', 'reverse');                      % Reverse y axis
hUpper.XAxis.Visible = 'off';    % Turn off x axis
hUpper.YLabel.String = 'Bar 2';  % Add a y label
hold on;
hBarUpper = bar(hUpper, 1:10, rand(1,10), ...  % A sample bar plot
                'FaceColor', 'none', 'EdgeColor', [1 0 0]);

% Add a legend:
hLegend = legend(hUpper, [hLine hBarLower hBarUpper], ...
                 {'line', 'bar 1', 'bar 2'}, 'Color', 'w');

以下是情节:

enter image description here

您可以根据需要重新定位图例,方法是左键单击它并将其拖动到最佳位置。