Need horizontal and vertical error bars to show on legend.

16 次查看(过去 30 天)
This is the relevant code for my problem:
figure; %Plot Cd vs. Cl
hold on;
errorbar(Cl(1,:),Cd(1,:),Cd_u(1,:),'Marker','.','Color','b'); %first line vertical
errorbar(Cl(1,:),Cd(1,:),Cl_u(1,:),'horizontal','Marker','.','Color','b','HandleVisibility','off'); %first line horizontal
errorbar(Cl(2,:),Cd(2,:),Cd_u(2,:),'Marker','.','Color','r'); %second line vertical
errorbar(Cl(2,:),Cd(2,:),Cl_u(2,:),'horizontal','Marker','.','Color','r','HandleVisibility','off'); %second line horizontal
errorbar(Cl(3,:),Cd(3,:),Cd_u(3,:),'Marker','.','Color','k'); %third line vertical
errorbar(Cl(3,:),Cd(3,:),Cl_u(3,:),'horizontal','Marker','.','Color','k','HandleVisibility','off'); %third line horizontal
title('Drag vs. Lift for various Re',FontSize=10);
xlabel('Lift Coefficient',FontSize=10);
ylabel('Drag Coefficient',FontSize=10); grid on;
legend('Re = 50k','Re = 100k','Re = 150k','location','best',FontSize=10);
hold off;
Note that this is the current iteration, but I've tried a few different things as I'll explain.
My issue is pretty specific. I am plotting drag against data, and both lift and drag have an uncertainty. I need to plot the drag against lift with the vertical errorbars representing the drag uncertainty, and horizontal errorbars representing the lift uncertianty. Doing this is simple enough, but the trick is there are 3 lines on the same plot, representing results under different environmental conditions. Obviously, I use the legend command to distingush them, however when I assign legend descriptions to each line on the plot it either:
a) Shows the first line with vertical bars, the first line with horizontal bars, then the second line with vertical bars. This is the result if I remove the "HandleVisibility","off" option from the horizontal errorbar plots.
b) Shows line 1 vertical bars, line 2 vertical bars, and line 3 vertical bars. This is the result with the code as shown, it's functional but the legend only dispays vertical errorbars, omitting the horizontal bars.
c) I've also tried setting a regular plot first with all three lines, then doing the errorbars, but this leaves the legend showing only the lines, and not the bars.
I know theoretically I could just live with it but I promise I need the horizontal bars to show. Is there any way I can make that happen?

采纳的回答

Voss
Voss 2023-10-26
% random data:
Cl = randn(3,10);
Cd = randn(3,10);
Cd_u = 0.1*randn(3,10);
Cl_u = 0.1*randn(3,10);
You can create an errorbar object that has two bars (vertical and horizontal) in a single call to errorbar().
figure; %Plot Cd vs. Cl
hold on;
colors = 'brk';
for ii = 1:size(Cl,1)
errorbar(Cl(ii,:),Cd(ii,:), ... x,y
Cd_u(ii,:),Cd_u(ii,:), ... yneg,ypos
Cl_u(ii,:),Cl_u(ii,:), ... xneg,xpos
'Marker','.', ...
'Color',colors(ii));
end
title('Drag vs. Lift for various Re',FontSize=10);
xlabel('Lift Coefficient',FontSize=10);
ylabel('Drag Coefficient',FontSize=10); grid on;
legend('Re = 50k','Re = 100k','Re = 150k','location','best',FontSize=10);
hold off;
Original plot for reference:
figure; %Plot Cd vs. Cl
hold on;
errorbar(Cl(1,:),Cd(1,:),Cd_u(1,:),'Marker','.','Color','b'); %first line vertical
errorbar(Cl(1,:),Cd(1,:),Cl_u(1,:),'horizontal','Marker','.','Color','b','HandleVisibility','off'); %first line horizontal
errorbar(Cl(2,:),Cd(2,:),Cd_u(2,:),'Marker','.','Color','r'); %second line vertical
errorbar(Cl(2,:),Cd(2,:),Cl_u(2,:),'horizontal','Marker','.','Color','r','HandleVisibility','off'); %second line horizontal
errorbar(Cl(3,:),Cd(3,:),Cd_u(3,:),'Marker','.','Color','k'); %third line vertical
errorbar(Cl(3,:),Cd(3,:),Cl_u(3,:),'horizontal','Marker','.','Color','k','HandleVisibility','off'); %third line horizontal
title('Drag vs. Lift for various Re',FontSize=10);
xlabel('Lift Coefficient',FontSize=10);
ylabel('Drag Coefficient',FontSize=10); grid on;
legend('Re = 50k','Re = 100k','Re = 150k','location','best',FontSize=10);
hold off;
  2 个评论
Josiah Kleven
Josiah Kleven 2023-10-26
Ok I didn't realize that I had to add top half and bottom half error, I had tried putting both into a single errorbar before and got weird looking bars, but it makes sense that it only plotted half of the bar.
Voss
Voss 2023-10-26
Yes. For a single errorbar, you can specify positive and negative error or you can specify one error value, in which case the errorbar is twice that long. For two errorbars at once, you have to specify positive and negative error values for both directions.
Any questions, let me know. Otherwise, if it solves the problem, don't forget to "Accept" this answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by