Incorrect rendering of legend in an error bar plot having both verticle and horizontal error bars.

7 次查看(过去 30 天)
I'm trying to plot variation of Reynold's stress as a function of cross-stream distance. There's an uncertainity in the measurement of both of these quantities and hence I need to show both horizontal as well as verticle errorbars. But I don't know why the symbols are not getting properly rendered here.
Would you please be kind enough to help me figure this out so that all the symbols are properly rendered.
For your convinence I've also attached the corresponding .fig file for the same.
Thanks in advance :-)

回答(2 个)

Walter Roberson
Walter Roberson 2024-3-11
You do not have 6 lines plus constant lines.
You have lots and lots of errorbar() objects, some of which happen to have the same color as other errorbar objects. Plus you have some constant lines.
You need to create some "fake" lines for legend purposes:
H(1) = line(nan, nan, 'Color', 'r', 'DisplayName', 'C1: Laden, \Xi ~ 1.00, Re = 2700.');
H(2) = line(nan, nan, 'Color', 'b', 'DisplayName', 'C1: Unladen, Re = 2700.');
and so on (making sure you get the colors right for the legend entries)
Then
legend(H)
  1 个评论
abhimanyu dubey
abhimanyu dubey 2024-3-16
First Of all Apologies for the late reply, and thanks a lot for your inputs and your time. :)
My aunt had suffered a stroke and have passed away recently, and hence I was on leave for few days, and didn't checked my mails.
I tried what you had suggested:
% Part of a much bigger plotting Code:
errorbar(ax1 , temp_Y_ , ...
temp_Umean , ...
yneg , ypos , ...
xneg , xpos , ...
data.symbol{i} + ":" , ... % Note to self: COLOUR earlier was {i+1}
'color' , data.color{i} , 'linew' , 1.5 , 'markersize' , 15) ;
H(i) = plot(nan , nan , ...
data.symbol{i} + "-" , ... %% Note to self: COLOUR earlier was {i+1}
'color' , data.color{i} , 'linew' , 1.5 , 'markersize' , 15) ;
% Call To Legend:
if isempty(leg_position)
lgd = legend( H , data.l{piv.selectLegend} , 'FontSize' , 13 , 'FontWeight' , ...
'bold' , 'location' , 'bestoutside', 'NumColumns' , num_of_leg_columns , 'Interpreter','latex') ;
else
if ~isempty(leg_fontsize)
lgd = legend( H , data.l{piv.selectLegend} , 'FontSize' , leg_fontsize , 'FontWeight' , ...
'bold' , 'location' , leg_position , 'NumColumns' , num_of_leg_columns , 'Interpreter','latex') ;
else
lgd = legend( H , data.l{piv.selectLegend} , 'FontSize' , 13 , 'FontWeight' , ...
'bold' , 'location' , leg_position , 'NumColumns' , num_of_leg_columns , 'Interpreter','latex') ;
end
end
And got the following results: ( I also understood why the way I was doing it was not working. For each point matlab was apparently creating a seperate graphics object to plot both horizontal and verticle error bars and hence the way I was doing it wasn't working. It worked when I had plotted just verticle error bars though)
This is not bad. But is there someway that I could get the symbols in the legend which shows both verticle and horizontal error bars as it was showing earlier ? I had tried doing this with error bar plot in a similar fashion but it didn't worked.

请先登录,再进行评论。


Voss
Voss 2024-3-16
编辑:Voss 2024-3-16
The basic problem is that you are creating more errorbar objects than you are making legend entries for. The solution is to make legend entries only for the particular errorbar objects you want to show up in the legend.
It's also possible that you are creating more errorbar objects than you intend. The number of errorbar objects created by each call to the errorbar function depends on the orientation and size of the inputs, as described in the errorbar documentation:
"MATLAB plots one line for each column in the matrices in these situations:
  • When all the coordinates and the error bar lengths are matrices of the same size and orientation
  • When all the inputs specified as vectors are the same length as the columns of the matrices
Otherwise, MATLAB plots one line for each row in the matrices."
("lines" plotted refers to errorbar objects created.)
Here's an example:
figure
% this creates 4 errorbar objects
err = 0.5;
h = errorbar([1;2;3;4],[4;3;2;1],err,err,err,err,'color','r','Marker','o')
h =
1×4 ErrorBar array: ErrorBar ErrorBar ErrorBar ErrorBar
size(h)
ans = 1×2
1 4
% and therefore the legend has 4 entries
legend
figure
% this creates 1 errorbar object
err = 0.5*[1,1,1,1];
h = errorbar([1,2,3,4],[4,3,2,1],err,err,err,err,'color','r','Marker','o')
h =
ErrorBar with properties: Color: [1 0 0] LineStyle: '-' LineWidth: 0.5000 Marker: 'o' XData: [1 2 3 4] YData: [4 3 2 1] XNegativeDelta: [0.5000 0.5000 0.5000 0.5000] XPositiveDelta: [0.5000 0.5000 0.5000 0.5000] YNegativeDelta: [0.5000 0.5000 0.5000 0.5000] YPositiveDelta: [0.5000 0.5000 0.5000 0.5000] Use GET to show all properties
size(h)
ans = 1×2
1 1
% and therefore the legend has 1 entry
legend
So if you want only one legend entry to represent the four errorbars in the first case, you have to specify which object to make legend entries for in the legend call:
figure
% this creates 4 errorbar objects
err = 0.5;
h = errorbar([1;2;3;4],[4;3;2;1],err,err,err,err,'color','r','Marker','o')
h =
1×4 ErrorBar array: ErrorBar ErrorBar ErrorBar ErrorBar
size(h)
ans = 1×2
1 4
% using h(1) as input to legend makes the legend have 1 entry
legend(h(1))
In your code, it's entirely possible you intend to create mulitple errorbar objects with each errorbar call, but I suspect that many more errorbar objects are created than intended with each errorbar call. Check how many errorbar objects are created by storing the output from errorbar in a variable and checking the size of that variable (as I've done with the variable h). If it is more than expected, you should check the orientation and size of the inputs to errorbar and adjust as necessary. Then, when creating the legend, only pass the handles to the particular errorbar objects that you want to have legend entries for (e.g., it might be only the first errorbar object created by each errorbar call).

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by