Why is my errorbar legend not showing the proper colors?
    12 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
I'm creating an errorbar plot, and I'm sure I did it correctly since the points changed color too. The legend is staying blue for both data sets though. Am I missing something? Thank you :)
box on
errorbar(data.Alkalinity_meq_L_(1:19), data.Salinity_ppt_(1:19), 0.8240, 'horizontal','.','Color','b')
hold on
errorbar(data.Alkalinity_meq_L_(21:24),data.Salinity_ppt_(21:24), 0.8240, 'horizontal','.','Color','r')
hold off 
xlabel('Alkalinity (meq/L)')
ylabel('Salinity (ppt)')
xlim([50 600])
ylim([2.5 25])
legend('Milk Lake','Goodenough Lake', 'Location', 'northwest','Orientation','vertical')
ax = gca;
set(gca,'FontSize',14)

1 commentaire
  Walter Roberson
      
      
 le 14 Mar 2024
				Same reason as https://www.mathworks.com/matlabcentral/answers/2092871-incorrect-rendering-of-legend-in-an-error-bar-plot-having-both-verticle-and-horizontal-error-bars#answer_1423491
Réponses (1)
  Voss
      
      
 le 14 Mar 2024
        
      Modifié(e) : Voss
      
      
 le 14 Mar 2024
  
      errorbar(x,y,err), with vector x and y and scalar err, produces as many errorbar objects as there are elements in x (or y).
In this case that gives you 19 blue errorbar objects and 4 red errorbar objects. When you call legend with two labels, the legend applies to the first two objects in your plot, which are the first two errorbars created, both of which are blue.
To fix this, you can create one blue errorbar object (containing 19 data points) and one red errorbar object (containing 4 data points) by specifying err as a vector the same size as x and y.
% some maade-up data
data = struct('Alkalinity_meq_L_',50+550*rand(1,24),'Salinity_ppt_',2.5+22.5*rand(1,24));
box on
errorbar(data.Alkalinity_meq_L_(1:19), data.Salinity_ppt_(1:19), 0.8240*ones(1,19), 'horizontal','.','Color','b')
hold on
errorbar(data.Alkalinity_meq_L_(21:24),data.Salinity_ppt_(21:24), 0.8240*ones(1,4), 'horizontal','.','Color','r')
hold off 
xlabel('Alkalinity (meq/L)')
ylabel('Salinity (ppt)')
xlim([50 600])
ylim([2.5 25])
legend('Milk Lake','Goodenough Lake', 'Location', 'northwest','Orientation','vertical')
ax = gca;
set(gca,'FontSize',14)
0 commentaires
Voir également
Catégories
				En savoir plus sur Errorbars dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
