How to change the order of bars, graphs and lines in a plot

92 views (last 30 days)
Tobi Schrei on 21 Oct 2016
Answered: DanielFromIllinois on 10 Aug 2021
Hello everybody,
actually I'm a little frustrated. I'm using Matlab 2012a and trying to get a plot with two y-axes, including bars according to the right y-axe and a graph according to the left y-axe. Moreover I insert a row of line()-Elements. The actual problem is, that in one figure the bars, graph and lines are cutting each other. Now I've got to reorder them to get the graph in front, than the line-elements and at least the bars in the background.
What's following is my code:
%Fourth Plot----------------------------------------------
Dist=[52.83 101.68 151.72 201.73 251.79 301.78 351.77 400.15 441.99];
FQ=[0.00 0.00 0.00 0.00 0.00 0.03 5.51 43.75 87.91];
figure(4); %4th figure
hold on;
[Ax,L1,B1]=plotyy(x,Dist,x,FQ,'plot','bar'); %left axe for distance, right axe for failure ratio
%scaling the axes
set(Ax(1),'ylim',[0 600]);
set(Ax(2),'ylim',[0 100]);
set(Ax(1),'ytick',[0 50 100 150 200 250 300 350 400 450 500 550 600]);
set(Ax(2),'ytick',[0 10 20 30 40 50 60 70 80 90 100]);
%color of the axes
%set(Ax(2),'YColor',[1 0 0]); -- doesn't work -- always colors both y-Axes
set(Ax(1),'YColor',[0 0 0]);
%Color of the graphs
set(L1(1),'Color',[0 0 1],'LineWidth',2);
set(B1,'FaceColor',[1 0 0],'BarWidth',0.3);
%text and legend
set(get(Ax(1),'Ylabel'),'String','Detektionsentfernung/cm');
set(get(Ax(2),'Ylabel'),'String','Ausfallquote/%');
xlabel('Messentfernung/cm');
title('Entfernungsmessung und Ausfallquote bei DYP-ME007Y');
legend('gemessene Entfernung','Ausfallquote','Location','NW');
grid on;
%drawing the ideal graph
line([0 500],[0 500],'Color',[0 0 0],'LineStyle',':');
line([50 50],[51 53],'LineWidth',2.0,'Color','m'); %Min-/Max-Werte
line([100 100],[99 102],'LineWidth',2.0,'Color','m');
line([150 150],[149 152],'LineWidth',2.0,'Color','m');
line([200 200],[198 202],'LineWidth',2.0,'Color','m');
line([250 250],[251 253],'LineWidth',2.0,'Color','m');
line([300 300],[301 304],'LineWidth',2.0,'Color','m');
line([350 350],[346 355],'LineWidth',2.0,'Color','m');
line([400 400],[181 406],'LineWidth',2.0,'Color','m');
line([450 450],[207 582],'LineWidth',2.0,'Color','m');
%trying to order
uistack(B1,'bottom');
uistack(L1,'top');
hold off;
I would be very appreciative, if anyone could help me. In addition... If anybody knows how to get one of the line-elements into the legend I would be happy too. Sry for all those questions, but I tried it for a lot of hours with a lot of tips from the internet...

DanielFromIllinois on 10 Aug 2021
I know this is a very old, non-answered question but. I found this answer on stackoverflow for answering how to reorder line objects in graphics. The below solution can be used generally anytime you have a graphic with children line objects or other types of axes children you want to reorder.
If you want to get just one of the line elements into the legend you can use the line property called 'DisplayName' and set that to something. It will automatically be added to a figure then.
For example:
line([50 50],[51 53],'LineWidth',2.0,'Color','m','DisplayName','Min/Max Values')

Categories

Find more on Annotations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by