2

That's how I draw two graphics (thanks for guys who helped me to do that):

clear
logsFolder = 'C:\logs\';
stocks = {'log'};

for stock = stocks
    filename = [logsFolder stock{1} '.log'];
    fLog = fopen(filename);
    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);

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

    lo1 = min(y);
    hi1 = max(y);
    lo2 = min(yPrice);
    hi2 = max(yPrice);

    if (hi2/lo2 > hi1/lo1)
      ylim(AX(1),[lo1 hi2/lo2 * lo1]);
      ylim(AX(2),[lo2 hi2]);
    else
      ylim(AX(1),[lo1 hi1]);
      ylim(AX(2),[lo2 hi1/lo1 * lo2]);
    end

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

    % iNeedToDrawThat = data{7}
end

Input file example is available here As you can see my file contains the last column which I also want to display. The range should be from 0 (at the bottom of figure) to the maximum value (at the up of the graph). So I need to draw three graphics somehow. It's ok to omit axis with labels for the third graph as I already have two axis and I have no place to add third one. However it's ok to "overlap" two axis if possible.

I have no idea how to do that so I'm looking for your help.

I've tried that but it doesn't work:

figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);

hold on;
volume = data{7};
plot(xindays, volume);
hold off;
Amro
  • 123,847
  • 25
  • 243
  • 454
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • What's the difference between what you want to do and "hold on"? – yhyrcanus Aug 10 '12 at 17:39
  • how to do that using `hold on`? i've just tried it but extra graphic doesn't appear. sorry i'm pretty novice to matlab so can ask easy things :) – Oleg Vazhnev Aug 10 '12 at 18:21
  • related question: [Plotting 4 curves in a single plot, with 3 y-axes](http://stackoverflow.com/q/1719048/97160) – Amro Aug 11 '12 at 08:20
  • @javapowered What you've tried with `hold on` should work, I've just tested it. What are you getting? – Eitan T Aug 22 '12 at 14:02

3 Answers3

3

I have already mentioned a similar question in the comments, it should give you plenty of ideas...

Anyway, I've put together a solution to plot multiple y axes. Right now the code is a bit involved, but it should be possible to refactor a re-usable function out of it (like the addaxis function on the File Exchange).

The idea is to first plot each curve in a separate axis (all superimposed), and make them transparent (except the bottom one). Next we create copies of this set of axes and shift them along the x-direction. We also make those copies transparent, but now we can show the tick-marks along the y-axis of each. Finally we give them correct z-order, and link the x and y limits so that we can use the pan and zoom functionality.

%# read and parse data from file
fid = fopen('log.log','rt');
C = textscan(fid, '%s %f %f %f', 'CollectOutput',true);
fclose(fid);
dt = datenum(C{1}, 'HH:MM:SS:FFF');
data = C{2};
NUM = size(data,2);

%# create a wider figure
hFig = figure('Position',get(0,'DefaultFigurePosition').*[1 1 1.7 1]);

%# some properties
clr = lines(NUM);
bgClr = get(0,'DefaultFigureColor');
pos = get(0,'DefaultAxesPosition');
pp = 0.1;   % shift in normalized units: pos(1)

%# create plot axes (make axis invisible)
hAx = zeros(NUM,1);
for i=1:NUM
    hAx(i) = axes('Parent',hFig, 'Color','none', ...
        'XColor',bgClr, 'YColor',bgClr, ...
        'Units','normalized', 'Position',pos+[(NUM-1)*pp 0 -(NUM-1)*pp 0]);
    line(dt, data(:,i), 'Color',clr(i,:), 'Parent',hAx(i))
end
axis(hAx, 'tight')  %# tight x/y limits

%# create shifted copies of axes to show y-ticks
hAxx = zeros(size(hAx));
for i=1:NUM
    hAxx(i) = copyobj(hAx(i), hFig);
    delete(get(hAxx(i),'Children'));    %# keep only axis
    set(hAxx(i), 'YColor',clr(i,:), ...
        'Units','normalized', 'Position',pos+[(NUM-i)*pp 0 -(NUM-i)*pp 0]);
    ylabel(hAxx(i), sprintf('Axis %d',i))
end
xlabel(hAxx(1), 'datetime')
title(hAxx(1), 'Log')
datetick(hAxx(1), 'x', 'HH:MM', 'keeplimits')

%# set 1st axis copy as current axis
set(hFig, 'CurrentAxes',hAxx(1))

%# adjust ticks of axes
set(hAx(1), 'Color','w')    %# give white bg to 1st axis
set(hAxx(1), 'XColor','k')  %# show xticks of 1st axis copy
set(hAxx(2:end), 'XTick',[], 'XTickLabel',[])
set(hAx, 'XTick',[], 'XTickLabel',[], 'YTick',[], 'YTickLabel',[])

%# fix z-order
for i=3:-1:1, uistack(hAxx(i),'top'), end
uistack(hAx(1), 'bottom')

%# link x/y limits so that panning/zooming works
lh = cell(NUM+1,1);
for i=1:NUM
    lh{i} = linkprop([hAxx(i);hAx(i)], 'YLim');
end
lh{end} = linkprop([hAxx;hAx], 'XLim');

The result:

screenshot

The panning/zooming is a bit funny, you have to initiate them by starting to drag from the side (the shifted colored axes). This is because the first one (corresponding to the blue line) is the one on top, thus catches all mouse clicks.

Note: I see you are using a custom function ticklabelformat, which I haven't tested in combination with the above code. I will leave that part to you..

HTH

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • thanks you just did it for me! yes it seems time labels are missing but I can leave without it... having third graph is better than having labels :) – Oleg Vazhnev Aug 22 '12 at 19:11
0

sample use of hold on

figure;
plot(x1,y1);
hold on
plot(x2,y2);
plot(x3,y3);
hold off

do "figure" and "hold on" only once outside the loop. then plot all the graphs you need

Gir
  • 839
  • 5
  • 11
  • i've updated description. i've tried to use `hold on` but it doesn't work. do you mean that I shouldn't use `plotyy` anymore and should only use `plot`? – Oleg Vazhnev Aug 10 '12 at 18:58
  • nope, it should work with all kinds of plots. i dont have matlab on my laptop so i cant test the code. http://www.mathworks.com/matlabcentral/answers/13743-how-to-plot-two-plotyy-plots-in-the-same-plot – Gir Aug 10 '12 at 19:00
0
figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);

hold on;
volume = data{7};
plot(xindays, volume);
hold off;

if you do it the way you suggested using hold on, i.e. use plotyy() first then the axes won't adjust so if you 3rd series is out of the range of your first set of axes then it won't appear. Try just flipping them and see if that produces a result?

volume = data{7};
plot(xindays, volume);

hold on;

[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);

This way the axes should adjust

For example:

t = 1:10;
x = t*2;
y = t*-2;
z = x + 1000;

Now compare

plot(t,z, 'r')
hold on
plotyy(t,x, t,y)

to

plotyy(t,x, t,y)
hold on
plot(t,z, 'r')
Dan
  • 45,079
  • 17
  • 88
  • 157