How can I add a marker to the legend with two x axes?
    조회 수: 2 (최근 30일)
  
       이전 댓글 표시
    
I have a plot with a single y axis and two axes (one on top and one on the bottom). However when I create a legend for the data, the marker is only showing for the dataset on the bottom x axis (pco2 data), and it is only showing the error bar without the marker for the dataset associated with the top axis (temperature).
This is the code I am using, with some example data so that it will run.
Thanks in advance!
Sean
pwa= [NaN;4.24217e+02;4.26400e+02;4.283015e+02;4.2992008e+02;4.314156e+02;4.330330e+02;4.348766e+02]
z = [4.25;3.75;3.25;2.75;2.25;1.75;1.25;0.75]
pwelog=[3.500037065180449e-04,0.001284843302444,0.002318105843833,3.758760966917231e-04,0.001427261282682,7.237037201646072e-04,0.001296985781426,0.001431607484257]
ssta=[NaN;15.717604391262380;15.749120466664706;15.804027153240781;15.850611733828360;15.910474411981033;15.995870332510352;16.141772469093860]
sste=[0.032302421912109,0.031360864698148,0.030310350321264,0.016022511508853,0.039655980079757,0.021463635973815,0.035459076323222,0.032660123519385]
figure();
hAx1=gca;      
hAx2=axes('Position',hAx1.Position, ...
          'XAxisLocation','top', ...
          'YAxisLocation','left', ...
          'Color','none', ...
          'YTick',[]); 
hold on
perr=errorbar(hAx1,log(pwa),z,pwelog,"horizontal","s","Color","blue", 'MarkerSize',9,"MarkerFaceColor","blue", "DisplayName","pCO_{2w}","LineWidth",1.5);
hold on
set(hAx2,'ydir','reverse');
set(hAx1,'ydir','reverse');
xlabel(hAx1,'ln(pCO_{2w}) (\muatm)');
ylabel(hAx1,'Depth (m)');
hold on
terr=errorbar(hAx2,ssta,z,sste,"horizontal","d","Color","red", 'MarkerSize',9,"MarkerFaceColor","red", "DisplayName","Temperature","LineWidth",1.5);
xlabel(hAx2,'Temperature (^{\circ}C)'); 
leg=legend([perr(1) terr(1)]);
leg.Location="northwest";
댓글 수: 0
채택된 답변
  Voss
      
      
 2024년 7월 23일
        Interestingly, swapping the order of perr and terr in the legend() call makes both markers show up in the legend.
pwa= [NaN;4.24217e+02;4.26400e+02;4.283015e+02;4.2992008e+02;4.314156e+02;4.330330e+02;4.348766e+02];
z = [4.25;3.75;3.25;2.75;2.25;1.75;1.25;0.75];
pwelog=[3.500037065180449e-04,0.001284843302444,0.002318105843833,3.758760966917231e-04,0.001427261282682,7.237037201646072e-04,0.001296985781426,0.001431607484257];
ssta=[NaN;15.717604391262380;15.749120466664706;15.804027153240781;15.850611733828360;15.910474411981033;15.995870332510352;16.141772469093860];
sste=[0.032302421912109,0.031360864698148,0.030310350321264,0.016022511508853,0.039655980079757,0.021463635973815,0.035459076323222,0.032660123519385];
Here's the result using the original code, but changing legend([perr(1) terr(1)]) to legend([terr(1) perr(1)]):
figure();
hAx1=gca;      
hAx2=axes('Position',hAx1.Position, ...
          'XAxisLocation','top', ...
          'YAxisLocation','left', ...
          'Color','none', ...
          'YTick',[]); 
hold on
perr=errorbar(hAx1,log(pwa),z,pwelog,"horizontal","s","Color","blue", 'MarkerSize',9,"MarkerFaceColor","blue", "DisplayName","pCO_{2w}","LineWidth",1.5);
hold on
set(hAx2,'ydir','reverse');
set(hAx1,'ydir','reverse');
xlabel(hAx1,'ln(pCO_{2w}) (\muatm)');
ylabel(hAx1,'Depth (m)');
hold on
terr=errorbar(hAx2,ssta,z,sste,"horizontal","d","Color","red", 'MarkerSize',9,"MarkerFaceColor","red", "DisplayName","Temperature","LineWidth",1.5);
xlabel(hAx2,'Temperature (^{\circ}C)'); 
leg=legend([terr(1) perr(1)]);
leg.Location="northwest";
Here's the same result with some additional unrelated code clean-up:
figure();
hAx1=gca;
hAx2=axes('Position',hAx1.Position, ...
          'XAxisLocation','top', ...
          'YAxisLocation','left', ...
          'Color','none', ...
          'YTick',[], ...
          'NextPlot','add');
perr=errorbar(hAx1,log(pwa),z,pwelog,"horizontal","s","Color","blue", 'MarkerSize',9,"MarkerFaceColor","blue", "DisplayName","pCO_{2w}","LineWidth",1.5);
terr=errorbar(hAx2,ssta,z,sste,"horizontal","d","Color","red", 'MarkerSize',9,"MarkerFaceColor","red", "DisplayName","Temperature","LineWidth",1.5);
set([hAx1 hAx2],'ydir','reverse');
xlabel(hAx1,'ln(pCO_{2w}) (\muatm)');
ylabel(hAx1,'Depth (m)');
xlabel(hAx2,'Temperature (^{\circ}C)'); 
legend([terr(1) perr(1)],'Location',"northwest");
댓글 수: 2
  Voss
      
      
 2024년 7월 23일
				You're welcome!
I can't think of a reason why this is the way it is, so I guess it's a bug.
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


