问题描述
在具有两个y轴的图形中,一个用于线图(左),另一个用于条形图(右),我希望条形图位于折线图的下方,以获得更好的可见性,而不是向上它.
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 instead of uper it.
正如您在这张图片上看到的(我希望您可以看到它),条形图显示了降水的演变,而不同的线显示了叶绿素指数的变化,我的问题是条形图覆盖了这些线条而且我希望线长一些.
As you can see on this picture (I hope you can see it), the bar chart shows the evolution of the precipitation and the different line the evolution of the chlorophyll index, my problem is that the bar-chart overlay the lines and I want the line to be uper.
这是我的脚本:
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轴)上,并将线图放在右轴上.我想将叶绿素指数保持在左侧.
Until now I only got half of the result. I want to put the bar-chart on the left axis (y-axis left) and the line plot on the right axis. I want to keep the Chlorophyll index on the left.
谢谢您的帮助
推荐答案
这实际上是一个非常好的问题,因为实际上没有明确记录的方式来做到这一点,在所有示例中,无论如何右轴显示在顶部.即使在示例在其自己的网站上,请注意他们在左轴上绘制bar
的谨慎程度.
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.
因此,作为序言,yyaxis
不会创建单独的轴(如yyplot
那样),而是而只是将一个NumericRuler
附加应用于相同轴.如果axes
完全不同,我们可以使用uistack
以我们想要的任何方式对轴进行重新排序,但是由于它们是 same axes
,因此我们需要仔细观察在axes
属性中,该属性控制内容的z顺序.
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.
当我们查看这些属性时,yyaxis
会自动将轴的SortMethod
从其默认值depth
更改为children
.这使得出现在左轴上的任何对象都在添加到右轴上的任何对象之下.因此,解决此问题所需要做的就是将SortMethod
更改回默认值(depth
),然后顺序将取决于z位置,就像通常在axes
中一样.
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);
现在,如果我们更改SortMethod
,它将使线对象位于顶部.
And now if we change the SortMethod
it brings the line object on top.
set(gca, 'SortMethod', 'depth')
这篇关于在左轴上绘制线图,在右轴上绘制条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!