Plot line-chart on the left axis uper the bar-chart on the right axis

后端 未结 3 1040
我在风中等你
我在风中等你 2020-12-11 21:27

In a graph with two y axis, one for a line plot (left) and one for a bar-chart (right), I would like that the bar-chart get under the line chart for a better visibility inst

相关标签:
3条回答
  • 2020-12-11 22:17

    Wrapping @Kouichi C. Nakamura's excellent answer up in a convenient function that applies this to all lines of an axis, gets:

    function ax = setZData(ax,val)
        ax = arrayfun(@(x)setZData_ax(x,val),ax);
    
        function ax = setZData_ax(ax,val)
            ax.ZData = val*ones(size(ax.XData));
        end
    end
    

    Or a complete wrapper

    function varargout = plotLeftOverRitht(LR,varargin)
    % wrapper to plot the left axis of dual-axis plot over the right axis
    
    %% process input
    % input string
    if strcmpi(LR,'left')
        LR = 'left';
        val = 1;
    elseif strcmpi(LR,'right')
        LR = 'right';
        val = 0;
    else
        error('plotLeftOverRitht:Input:LR',"The first input must be either 'left' or 'right'.")
    end
    
    % input: axis handle?
    if isa(varargin{1},'matlab.graphics.axis.Axes')
        ax = varargin{1};
        varargin = varargin(2:end);
    else
        ax = gca;
    end
    
    %% main function
    % activate left axis
    yyaxis( ax, LR);
    % call plot
    lines = plot(   ax, varargin{:});
    % set height
    setZData(lines,val);
    % set sorting order
    set(ax, 'SortMethod', 'depth')
    
    %% output
    if nargout > 0
        varargout = ax;
    end
    end
    

    This changes example above to be

    days = 0:5:35;
    conc = [515 420 370 250 135 120 60 20];
    
    figure
    plotLeftOverRitht('left', days,conc, 'LineWidth',4)
    plotLeftOverRitht('right', days,fliplr(conc), 'LineWidth',4)
    
    0 讨论(0)
  • 2020-12-11 22:29

    This was very helpful. Just to provide more examples, in case of two line plots, you'll need to explicitly specify ZData property to control the stack order.

    days = 0:5:35;
    conc = [515 420 370 250 135 120 60 20];
    
    figure
    
    yyaxis left
    p1 = plot(days, conc, 'LineWidth', 4);
    
    yyaxis right
    p2 = plot(days, fliplr(conc), 'LineWidth', 4);
    
    % orange is on top
    

    get(gca,'SortMethod')
    set(gca, 'SortMethod', 'depth')
    
    % orange is still on top
    

    p1.ZData = ones(size(p1.XData));
    p2.ZData = zeros(size(p2.XData));
    
    % blue is on top
    

    0 讨论(0)
  • 2020-12-11 22:33

    This is actually a really great question because there is really no clearly documented way of doing this and in all examples, whatever is on the right axes is displayed on top. Even in this example on their own website, notice how careful they are to plot the bar on the left axes.

    So as a preface, yyaxis does not create a separate axes (as yyplot used to), but rather just applies an additional NumericRuler to the same axes. If it were simply different axes we could use uistack to re-order the axes in any way we want, but because they are the same axes, we need to look a little closer at the axes properties which control the z-ordering of the contents.

    When we look at these properties, yyaxis automatically changes the SortMethod of the axes to children from it's default value of depth. This makes any object which appears in the left axes to be below anything added to the right axes. So all we need to do to fix this, is to change the SortMethod back to the default value (depth) and then the ordering will be dependent upon z position like it normally would within an axes.

    So as a demonstration, let's create some data

    days = 0:5:35;
    conc = [515 420 370 250 135 120 60 20];
    temp = [29 23 27 25 20 23 23 17];
    

    Create the plots just like you did (a line and a bar with the bar on the right)

    yyaxis right
    b = bar(days, temp, 'FaceColor', [0.8 0.8 0.8]);
    yyaxis left
    p = plot(days, conc, 'LineWidth', 2);
    

    And now if we change the SortMethod it brings the line object on top.

    set(gca, 'SortMethod', 'depth')
    

    0 讨论(0)
提交回复
热议问题