Azzera filtri
Azzera filtri

I plotted 2 plots each with error bars, but the legend is not showing the symbols correctly, how to solve it?

35 visualizzazioni (ultimi 30 giorni)
a = [1:5 ];
b = [6:10];
x = [1:5 ];
mu1 = mean(a);
cig1 = std(a);
errlow1 = cig1;
errhigh1 = cig1;
mu2 = mean(b);
cig2 = std(b);
errlow2 = cig2;
errhigh2 = cig2;
plot(x,mu1,'b-o','LineWidth',3)
hold on
errorbar(x,mu1,errlow1,errhigh1,'Color','k')
hold on
plot(p,mu2,'g-o','LineWidth',3);
hold on
errorbar(x,mu2,errlow2,errhigh2'Color','k');
hold off
legend( 'With Catheter' , 'Without Catheter' );
xlabel('Pressure in MPa')
ylabel('Cavitation Signal in V')
set(gca,'FontSize',20,'FontWeight','bold')
grid on

Risposta accettata

dpb
dpb il 4 Apr 2023
Modificato: dpb il 6 Apr 2023
a = [1:5 ];
b = [6:10];
x = [1:5 ];
mu1 = mean(a);
cig1 = std(a);
err1 = cig1;
mu2 = mean(b);
cig2 = std(b);
err2 = cig2;
hEB1=errorbar(x,mu1*ones(size(x)),err1*ones(size(x)),'kx-');
hold on
hEB2=errorbar(x,mu2,err2,'rx-');
legend([hEB1(1) hEB2(1)],{'With Catheter','Without Catheter'},'location','northoutside');
xlabel('Pressure in MPa')
ylabel('Cavitation Signal in V')
grid on
When plotting a constant y versus a vector x, the line won't be shown connecting the points and you'll get an array of line/errorbar handles instead of just one for each call. This is also true with the error values for the errorbar plot. The fix for the legend is then to save the handles for the two calls and use one of those for each to associate the legend with the proper linestyle.
Alternatively, duplicate the constant values by the number of elements in the vector as the other poster noted; but you don't need to reorder, just be sure to put the labels on in the right order and with the associated handle. If use vectors, then as noted there will only be a single errorbar object handle for each call (or, if you put the two response vectors in to an array by column, the columns will be treated as independent variables).
HOWEVER, all the above is well and good, but...
One would assume that in reality there would be observed data for each of the points rather than the obviously contrived example; if that's the case then just use it instead of the mean and all will work out as wanted automagically...we'll just use the above even though contrived...
figure
errorbar(x,a,err1*ones(size(x)),'kx-');
hold on
errorbar(x,b,err2*ones(size(x)),'rx-');
legend({'With Catheter','Without Catheter'},'location','northoutside');
xlabel('Pressure in MPa')
ylabel('Cavitation Signal in V')
xlim([0.5 5.5])
grid on
  1 Commento
dpb
dpb il 6 Apr 2023
x=1:5; a=x; err=std(a);
errorbar(x,a,err,'kx-');
hold on
errorbar(x,a+5,err(ones(size(x))),'rx')
xlim(xlim+0.5*[-1 1])
grid on
leaves unconnected errorbars; I think a worthwhile enhancement to errorbar would be treat a single value for the error magnitude as the vector. Many other MATLAB functions have automagic expansion where it makes sense (and in some cases where it can be a puzzle as to what happened besides), this seems a place that would be the expected result.
As the second above shows, if the line weren't wanted, it can be surpressed by the linestyle triplet omitting the line character or explicit use of the 'linestyle' property so no functionality would be lost by the gain in user convenience.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by