Issues creating error bar for bar figure

18 visualizaciones (últimos 30 días)
Kristine
Kristine el 11 de Sept. de 2025 a las 1:21
Editada: Matt J el 11 de Sept. de 2025 a las 15:46
Hi,
I'm trying to create error bars on my bar plot.
I get the error: "Input arguments must be numeric, datetime, duration, or categorical."
I'm not sure what I'm doing wrong. Even when I make err equal to two numbers it still doesn't work.
AMean = 656631
AMean = 656631
BMean = 1130
BMean = 1130
ASTD = 237027
ASTD = 237027
BSTD = 209
BSTD = 209
AHeight = 10
AHeight = 10
BHeight = 11
BHeight = 11
Names = ["A"; "B" ] ;
Averages = [AMean; BMean] ;
StandDev = [ASTD ; BSTD] ;
SampSize = [AHeight; BHeight] ;
NewTable = table(Names, Averages, StandDev, SampSize) ;
x = NewTable.Names ;
y = NewTable.Averages ;
err = StandDev ./ sqrt(SampSize) ;
bar(x, y)
errorbar(x,y,err)
Error using errorbar (line 86)
Input arguments must be numeric, datetime, duration, or categorical.

Respuesta aceptada

Matt J
Matt J el 11 de Sept. de 2025 a las 1:46
Editada: Matt J el 11 de Sept. de 2025 a las 1:52
Here's an MWE:
x = ["A"; "B" ] ;
y = [3,4] ;
err = [0.5,0.75] ;
bar(x, y); hold on
errorbar(categorical(x),y,err,'r', 'Linestyle','none');
axis padded
  4 comentarios
Chuguang Pan
Chuguang Pan el 11 de Sept. de 2025 a las 14:13
@Kristine. In order to remove a line connecting, you can use Linestyle = 'none' as shown in the code:
errorbar(categorical(x),y,err,'r', 'Linestyle','none');
Matt J
Matt J el 11 de Sept. de 2025 a las 15:44
Editada: Matt J el 11 de Sept. de 2025 a las 15:46
But it would be very strange to connect the bars with lines. Lines on a plot are meant for visualizing increases and decreases of y with x. But here, there is notion of "increasing x". The x-axis just contains unordered bar labels A and B.

Iniciar sesión para comentar.

Más respuestas (1)

Chuguang Pan
Chuguang Pan el 11 de Sept. de 2025 a las 1:51
The problem lies in the variable Names is string array, which is not supported by errorbar function. You can use categorical function to convert string array to categries.
AMean = 656631;
BMean = 1130;
ASTD = 237027;
BSTD = 209;
AHeight = 10;
BHeight = 11;
Names = ["A"; "B" ] ;
Averages = [AMean; BMean] ;
StandDev = [ASTD ; BSTD] ;
SampSize = [AHeight; BHeight] ;
NewTable = table(Names, Averages, StandDev, SampSize) ;
x = NewTable.Names ;
y = NewTable.Averages ;
err = StandDev ./ sqrt(SampSize) ;
bar(x, y)
hold on
errorbar(categorical(x),y,err)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by