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

后端 未结 3 1047
我在风中等你
我在风中等你 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)
    

提交回复
热议问题