Different error bar color than the plot

How can I set the error bar's color, thickness in the plot?
As you can see the error bar has the same color with the plot. How can I set the error bar color black and lower thickness?
the code I am using for this figure:
col = jet(length(KsTable.k_name));
errorbar(xy.xy1,abs(xy.xy2),xy.xy3,'-rs','Color',col(jjj,:),'LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','r',...
'MarkerSize',5)

Answers (1)

Adam Danz
Adam Danz on 31 Oct 2019
Edited: Adam Danz on 1 Nov 2019
You can plot the main line and the error bars separately. Here's a demo that plots the main line first and then the errorbars. If you want the errorbars under the main line, plot the errorbars first.
x = 0:.2:1;
y = x.^2;
err = y./5;
plot(x,y,'ro-')
hold on % <--Put this outside of your loop)
eb = errorbar(x,y,err,'LineStyle','none', 'Color', 'k','linewidth', 2);

8 Comments

I dont see option in your code to control the plot and error bar separately. As I said I wan the error bar to be black however I want to be able to change the color of my plot.
Adam Danz
Adam Danz on 3 Nov 2019
Edited: Adam Danz on 21 Nov 2019
That's exactly what my answer does and that's exactly what the figure shows in my answer.
Have you tried running the code? The 4th line controls the color/style of the trend line (red) while the 6th line controls the color/style of the errorbars (black).
Please take a moment to understand what each line of the code is doing. In my answer, there are only 6 lines and only 2 of them really matter. This forum can help you solve problems but it can't think for you.
Thanks Adam
Your code saved me time :)
I know its a little off topic, but I am wondering if it is possible to change color of error bars that are outside a threshold? I've thought of breaking up the error bars into 2 arrays and plotting them separately like above, but that just seems like a lot of work. Would be nice if Matlab had something more built-in.
I recommend plotting 2 sets of errorbars. The first set will include all of the data. The second will only include error bars that exceed threshold and you can set their color and either remove the vertical connector bar or set LineStyle to None to remove the trendline.
x = 1:5;
y = 1:5;
err = [3 2 2 3 3];
idx = err > 2;
hold on
errorbar(x,y,err, 'k-', 'LineWidth', 2)
errorbar(x(idx),y(idx),err(idx), 'r-', 'LineWidth', 2, 'LineStyle','none')
Sweet, thanks for the response! Would be cool to have this ability more "natively" in the error bar function, but this was the solution I was thinking of (and simpler than I thought).
How can I get filled markers here, instead of empty?

Sign in to comment.

Asked:

on 31 Oct 2019

Commented:

on 18 Nov 2022

Community Treasure Hunt

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

Start Hunting!