Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate color errorbar matlab

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...

like image 523
user1234440 Avatar asked Apr 01 '14 22:04

user1234440


2 Answers

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
like image 178
Luis Mendo Avatar answered Sep 26 '22 15:09

Luis Mendo


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
like image 38
Petri Mustonen Avatar answered Sep 22 '22 15:09

Petri Mustonen