How can I add a marker to the legend with two x axes?

17 次查看(过去 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]
pwa = 8x1
NaN 424.2170 426.4000 428.3015 429.9201 431.4156 433.0330 434.8766
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
z = [4.25;3.75;3.25;2.75;2.25;1.75;1.25;0.75]
z = 8x1
4.2500 3.7500 3.2500 2.7500 2.2500 1.7500 1.2500 0.7500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
pwelog=[3.500037065180449e-04,0.001284843302444,0.002318105843833,3.758760966917231e-04,0.001427261282682,7.237037201646072e-04,0.001296985781426,0.001431607484257]
pwelog = 1x8
0.0004 0.0013 0.0023 0.0004 0.0014 0.0007 0.0013 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ssta=[NaN;15.717604391262380;15.749120466664706;15.804027153240781;15.850611733828360;15.910474411981033;15.995870332510352;16.141772469093860]
ssta = 8x1
NaN 15.7176 15.7491 15.8040 15.8506 15.9105 15.9959 16.1418
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sste=[0.032302421912109,0.031360864698148,0.030310350321264,0.016022511508853,0.039655980079757,0.021463635973815,0.035459076323222,0.032660123519385]
sste = 1x8
0.0323 0.0314 0.0303 0.0160 0.0397 0.0215 0.0355 0.0327
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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";

采纳的回答

Voss
Voss 2024-7-23,17:52
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 个评论
Sean Morgan
Sean Morgan 2024-7-23,17:55
Interesting. That did seem to work! Do you know why or is it just a strange bug?
Thanks very much!
Sean
Voss
Voss 2024-7-23,18:06
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 CenterFile Exchange 中查找有关 Errorbars 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by