Newbie: Order of line and plot in a simple figure

11 views (last 30 days)
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

Accepted Answer

TADA
TADA on 30 Mar 2019
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 Comments
madhan ravi
madhan ravi on 31 Mar 2019
Wünderschön, endlich habt ihr geschaffen! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!