Newbie: Order of line and plot in a simple figure

조회 수: 11 (최근 30일)
Fidelino83
Fidelino83 2019년 3월 30일
댓글: TADA 2019년 3월 31일
Sorry for asking stupid questions, but I'm not getting it
Main Problem: Bar chart is always overlaying line plot
Month = [1 2 3 4 5 6 7 8 9 10 11 12]
Temp = [15.8 15.8 15.8 15.7 15.8 15.1 14.7 14.6 15.0 15.7 16.2 16.1]
Prec = [88.0 108.4 129.3 169.5 197.3 212.9 204.3 145.6 121.0 116.2 101.3 99.9]
left_color = [0 0 0]
right_color = [0 0 0]
set(figure(1),'defaultAxesColorOrder',[left_color; right_color]);
hold
xticks([1 2 3 4 5 6 7 8 9 10 11 12])
xlabel ('Monate')
h1=plot(Month,Temp,'color','r')
axis ([1 12 0 30])
yyaxis right
axis ([1 12 0 300])
h2=bar(Month,Prec,'b')
I know, this alreasy looks confused, because I am, I changed everything thousand times, now I'm absolutely coinfus.
What I was trying to do:
Plotting a line graph and bars in oine figure. Line graphe in foreground.
Left yaxis for the Line Graph
Right yaxis for the bars
bars blue, line red
So far, everythign worked but I'm not managing to bring the Line graph to the foreground
Help needed. Thank you

채택된 답변

TADA
TADA 2019년 3월 30일
This is actually not a newbie issue...
As far as I know, it's a known issue with yyaxis.
A workaround by @Charles Brown using two axes taken from here works very well
figure(42);
clf();
Month = [1 2 3 4 5 6 7 8 9 10 11 12];
Temp = [15.8 15.8 15.8 15.7 15.8 15.1 14.7 14.6 15.0 15.7 16.2 16.1];
Prec = [88.0 108.4 129.3 169.5 197.3 212.9 204.3 145.6 121.0 116.2 101.3 99.9];
% first init the left & right axes, give both the same position
% left axis needs to be created second to be on top.
rightAx = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
leftAx = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
xticks([1 2 3 4 5 6 7 8 9 10 11 12]);% not necessary really
xlabel ('Monate');
h1=plot(Month,Temp,'color','r');
axis ([0 13 0 30]);
% yyaxis right
subplot(rightAx);
h2=bar(Month,Prec,'b');
% Need to move the Y-axis after plotting the foreground data for some
% reason.
rightAx.YAxisLocation = 'right';
% Now we have to set the background of the front axis to be transparent.
set(leftAx, 'Color', 'None');
still need some playing with the tick marks but it's a good start
  댓글 수: 9
madhan ravi
madhan ravi 2019년 3월 31일
Wünderschön, endlich habt ihr geschaffen! :)
TADA
TADA 2019년 3월 31일
Cheers @Fidelino! :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by