Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

how to draw 2 graphics on the same plot so I can compare them

I have two financial graphics and I need to analyze how them perform comparing to each other. I wrote such program:

clear

fLog = fopen('log.log');
data = textscan(fLog, '%f:%f:%f:%f %f %f %f');
fclose(fLog);

% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';

y = data{5};
yPrice = data{6};

xindays = x / (24*60*60);

plot(xindays, y);
hold on
plot(xindays, yPrice);

ticklabelformat(gca,'y','%g')
ticklabelformat(gca,'x',{@tick2datestr,'x','HH:MM:SS'})

log.log file example

The problem is that as two graphics has completely different "Y" value them both looks like "straight" line. So I need somehow to have two Y axis, probably one on the left and one on the right, but they should has the same scale, meaning 1% change should be the same on both graphics so I should be able to compare which stock outperforms and where. If it is possible to do that and how?

I also want to draw this graphics using different colors but I likely can google myself how to do that once above problem is resolved.

Upvotes: 0

Views: 741

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

If it is simply about outperforming try this: suppose the values are in vectors x and y, get the relative change by : x_rel=x/mean(x) y_rel=x/mean(y)

now 1% change (compared to the mean) will show exactly the same change in the graph.

Upvotes: 1

Gunther Struyf
Gunther Struyf

Reputation: 11168

You're looking for plotyy. Because you also change the xticks, I advise to remove the xticks from one of the two resulting axes, otherwise they'll overlap:

[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
set(AX(1),'xtick',[])

ticklabelformat(AX(2),'y','%g')
ticklabelformat(AX(2),'x',{@tick2datestr,'x','HH:MM:SS'})

This gives you the following figure:

enter image description here

EDIT:

As you can see, plotyy just makes sure that all data is in the visible window. So the y-axis scalings are not equal. If you really want that, you can always change that after using plotyy. eg:

[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
set(AX(1),'xtick',[]);

ylim(AX(1),[<lower bound>   <upper bound>]);
ylim(AX(2),[<lower bound>   <upper bound>]);

ticklabelformat(AX(2),'y','%g')
ticklabelformat(AX(2),'x',{@tick2datestr,'x','HH:MM:SS'})

The ideal visible ranges depend on the range of the data itself, so you'll have to fix this yourself.

Upvotes: 1

Related Questions