How to separate color errorbar matlab
Asked Answered
B

2

8

Given the following example code:

x = 0:pi/10:pi;
y = sin(x);
e = std(y)*ones(size(x));

figure
errorbar(x,y,e)

How are you able to color the line different compared to the horizontal lines?

I tried

errorbar(x,y,e,'--mo')

But this changes all of them together...

Baecher answered 1/4, 2014 at 22:11 Comment(0)
R
11

Get a handle to the errorbar object. It has two children, corresponding to the data plot and error bars respectively. Then you can set the color of each separately.

h = errorbar(x,y,e) %// a color spec here would affect both data and error bars
hc = get(h, 'Children')
set(hc(1),'color','b') %// data
set(hc(2),'color','g') %// error bars
Ragsdale answered 1/4, 2014 at 22:16 Comment(1)
awesome, this is much simpler to the linespec mumbo jumboBaecher
R
3

In 2014b the error bar object doesn't have children anymore. One (ugly) way of circumventing this is to plot the function again with a different color. Effectively this plots the function with a new color on top of the function with the old color.

hold on;
errorbar(x, y, e, 'r'); % // The color here will stay for the error bars
plot(x, y, 'b');        %// Here we change the color of the original function
Rule answered 18/1, 2015 at 14:32 Comment(1)
You can hide the lineplot in errorbar at all: errorbar(x, y, e, 'r', 'LineStyle', 'none');Koheleth

© 2022 - 2024 — McMap. All rights reserved.