[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Speeding up a plotting function
From: |
Francesco Potortì |
Subject: |
Re: Speeding up a plotting function |
Date: |
Thu, 03 Nov 2016 11:40:22 +0100 |
>I have written a simple plotting function, code below, but unfortunately it
>takes some time to plot. Is there any way in which I could speed this up?
>
>function hilo_conditional_plot( high , low , condition )
>%HILO_CONDITIONAL_PLOT
>% Takes high, low and condition input vectors and plots a line chart of
>highs
>% and lows coloured according to the condtion. For this basic version there
>% are only 3 conditons; 1 for long, -1 for short and 0 for neutral; with the
>% respective plot colours being blue, red and green.
>
>date = ( 1 : length(high) )' ;
>hold on ;
>for ii = 1 : length( high )
> if condition(ii) == 1
> line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'b'
> ,'linewidth' , 2 ) ;
> elseif condition(ii) == -1
> line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'r'
> ,'linewidth' , 2 ) ;
> elseif condition == 0
> line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'g'
> ,'linewidth' , 2 ) ;
> else
> printf( 'Error in condition vector - a value != 1,-1 or 0' ) ;
> end
>end
>grid minor on ;
>hold off ;
Dear babelproofreader,
as already suggested, the likely problem is that you call the line()
function a lot of times, which slows things down. I suggest you do just
three calls to plot(), which should be much faster. With plot, you can
draw a discontinuous line by including NaN values.
Assuming that low and high are column vectors, it would be something
like that (untried, may contain bugs):
hole = nan(size(date));
xall = [date date hole];
yall = [low high hole];
hold on
for c = [-1 0 1]
filt = (condition == c);
x = xall(filt, :)';
y = yall(filt, :)';
color = "brg"(c+2);
plot(x(:), y(:), 'color", color, 'linewidth", 2);
endfor
hold off
condition values not in [-1 0 1] are ignored.
--
Francesco Potortì (ricercatore) Voice: +39.050.621.3058
ISTI - Area della ricerca CNR Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa Skype: wnlabisti
(entrance 20, 1st floor, room C71) Web: http://fly.isti.cnr.it