在左轴上绘制折线图,​​在右轴上绘制条形图

时间:2016-06-08 18:17:14

标签: matlab plot bar-chart

在一个有两个y轴的图表中,一个用于线图(左),一个用于条形图(右),我希望条形图位于折线图下方以获得更好的可见度而不是uper它

Evolution bar-chart line-plot

正如你在这张照片上看到的那样(我希望你能看到它),条形图显示了降水的演变和叶绿素指数演变的不同线,我的问题是条形图覆盖了线条我想让这条线变得更好。

这是我的剧本:

figure
yyaxis right
bar (meteo(:,1),meteo(:,14));
ylabel('Precipitation (mm)');
hold on
for i=1:6;
    a = [];
    b = [];
    color = ['r' 'm' 'b' 'c' 'g' 'y'];
    for j=0:6
        a(i,j+1)=matrice_5(j*6+i,1);%jour
        b(i,j+1)=matrice_5(j*6+i,3);%moyenne       
    end
        hold on
        yyaxis left
        plot(a(i,:),b(i,:),color(i),'LineWidth',1.5);  
end
title('Evolution of the mean of the chlorophyll index (HNT) - Charlotte variety');
xlabel('Day (2013)')
ylabel('Chlorophyll index (HNT)')
axis([735390 735442 32 50]);
set(gcf,'Position',[645 206 701 477]);
datetick('x','dd mmm','keepticks')
h=legend('0','50','100','150','200','250','precipitation','Location','best');
v = get(h,'title');
set(v,'string','Nitrogen rate in kg/ha');
set(h,'Position', [0.1793 0.1494 0.1127 0.2446]);
hold on
plot([735422 735422],[32 49],'Color',[.3 .3 .3]);
hold off

到目前为止,我只获得了一半的结果。我想将条形图放在左轴(左轴为y轴),将线图放在右轴上。我想保留左边的叶绿素指数。

感谢您的帮助

3 个答案:

答案 0 :(得分:4)

这实际上是一个非常好的问题,因为实际上没有明确记录这样做的方法,并且在所有示例中,右轴上的任何内容都显示在顶部。即使在他们自己网站上的这个example中,也要注意他们在左轴上绘制bar是多么小心。

因此,作为前言,yyaxis不会创建单独的轴(如yyplot所用),而是rather just applies an additional NumericRuler to the same axes。如果它只是不同axes,我们可以使用uistack以我们想要的任何方式重新排序轴,但因为它们是相同的 axes,我们需要仔细查看控制内容z顺序的axes属性。

当我们查看这些属性时,yyaxis会自动将SortMethod轴更改为children,其默认值为depth。这使得出现在左轴上的任何对象都低于添加到右轴的任何对象。所以我们需要做的就是将SortMethod更改回默认值(depth),然后排序将依赖于z位置,就像它通常在{{1}内一样}}

作为演示,让我们创建一些数据

axes

创建与你一样的图(一条线和一条带右侧栏的栏)

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

enter image description here

现在,如果我们更改yyaxis right b = bar(days, temp, 'FaceColor', [0.8 0.8 0.8]); yyaxis left p = plot(days, conc, 'LineWidth', 2); ,它会将行对象置于最顶层。

SortMethod

enter image description here

答案 1 :(得分:0)

这非常有帮助。仅提供更多示例,在有两个折线图的情况下,您需要显式指定ZData属性以控制堆栈顺序。

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

orange is on top

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

% orange is still on top

orange is on top

p1.ZData = ones(size(p1.XData));
p2.ZData = zeros(size(p2.XData));

% blue is on top

blue is on top

答案 2 :(得分:0)

在方便的函数中将@Kouichi C. Nakamura包裹起来,将其应用于轴的所有行,得到:

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

或完整的包装器

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

将上面的示例更改为

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)
相关问题