Why are my error bars multicoloured with no line?

6 次查看(过去 30 天)
When I run this very simple code the graph plotted has multicoloured error bars and no joining line? Why.
figure(24)
x = 20:1:25;
y = [1.75 1.78 1.81 1.85 1.9 1.95];
err = 0.1;
errorbar(x,y,err)

回答(2 个)

VBBV
VBBV 2024-4-21
x = 20:1:25;
y = [1.75 1.78 1.81 1.85 1.9 1.95];
err = repmat(0.1,length(x),1);
errorbar(x,y,err)
  3 个评论
Adam Danz
Adam Danz 2024-4-22
@Kristiaan Winkel reminder to accept VBBV's answer if this addressed your question.

请先登录,再进行评论。


Adam Danz
Adam Danz 2024-4-22
编辑:Adam Danz 2024-4-22
Understanding array size compatibility
errorbar, like plot, supports mixed array sizes in input arguments as long as the sizes are compatible. This feature was added to errorbar in R2022b. Array size compatibility means that elements of one argument have a correspondence to elements of another argument. For example, one argument can be a 2x3 array and another argument can be a 1x2 or 1x3 vector.
For example, the first argument below is 3x2 and the second argument is 1x2. This tells MATLAB to map the vector values to the columns of the matrix. It produces 3 objects because the rows are the unmatched dimension and there are 3 rows.
errorbar([1 2; 3 4; 5 6], [.4 .9]);
axis padded; grid on
Now, make a small changed. Set the second argument to a 3x1 vector. Now the MATLAB will map the vector values to the rows of the matrix. It produces 2 objects because now the columns are the unmatched dimension and there are 2 columns.
figure()
errorbar([1 2; 3 4; 5 6], [.4 .9 .3]);
axis padded; grid on
In the exampled posted by the OP, the third argument is a scalar but the first and second arguments are 1x6 vectors. The 1x1 scalar maps to the number of rows in the other arguments. Six objects are produced because there are 6 columns of data in the unmatched dimension. Since each object only has 1 point, there are no connector lines.
figure()
x = 20:1:25;
y = [1.75 1.78 1.81 1.85 1.9 1.95];
err = 0.1;
errorbar(x,y,err)
axis padded; grid on
As @VBBV explained, to produce a single object instead of six objects, specify the error for each value of x and y.
figure()
x = 20:1:25;
y = [1.75 1.78 1.81 1.85 1.9 1.95];
err = 0.1;
errorbar(x,y,err.*ones(size(x)))
axis padded; grid on
Is this something readers would like to learn more about? I wonder if a blog article on this topic would be useful. Please let me know if anyone has questions.

类别

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