Matlab: Indicate on a plot when data is off the plot? - matlab

Is there a way to have a mark on a plot axis for any data points off the current plot in Matlab? It is great if the method works with zoom and pan, but I can live without it.
I have several data sets that I am plotting using subplots. Each plot contains 20 data points, similar to marking where darts landed on a dart board. For most data sets, these plots are within a standard range, ie the size of the dart board, so I am using this standard for the axis range of each plot to make the subplots easily visually comparable. However, a few data sets have an outlier that is outside this standard range (typically way outside). I do not want to change the axis to show the outlier, but I want the user to be aware that there is 1 or more data points off the plot, and preferably in which direction they are off.
I thought a bit about trying to use the axis markers (set(gca, 'Ytick', xLowerLimit: markSpacing: xUpperLimit)) and adding an extra marker in a different color to indicate the location of an outlier, but I couldn't see how to do this without disrupting the regular markers and in a automated way that could allow for multiple outlier marks.

One approach is to see if there is any data that exceed your Y Axis limit and then place some text somewhere to notify the user. Options include text (puts text on axis) or uicontrol (puts text somewhere in figure window).
Here is an example using uicontrol:
% Set up figure window and axes
h.f = figure;
h.ax = axes('Units', 'Normalized', 'Position', [0.13 0.2 0.75 0.75]);
% Plot some sample data and window our axes to replicate the question
x = 1:10;
y = [1:9 20];
xmin = 0; xmax = 10;
ymin = 0; ymax = 10;
plot(h.ax, x, y);
axis(h.ax, [xmin xmax ymin ymax]);
% Generate logical matrix to find if any y data is beyond our axis limit
ymask = y > ymax;
if any(ymask) % Loop triggers if any y value is greater than ymax
str = sprintf('There are %u data points greater than current y axis limit', sum(ymask));
uicontrol('Parent', h.f, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.01 0.01 0.9 0.05], ...
'String', str ...
);
end
Which generates the following (annotations added manually):
This can be extended to other directions with some simple copy + paste and tweaks.

Edit: see bottom for improved method using callbacks.
You can find data points exceeding the axes limits and draw them on the limits, using different symbols:
A = randn(20, 2);
x_range = [-1.5, 1.5];
y_range = [-1.5, 1.5];
figure(1);
clf;
ah = axes;
plot(ah, A(:,1), A(:,2), 'o')
hold on
set(ah, 'XLim', x_range, 'YLim', y_range)
x_lt = A(:,1) < x_range(1);
x_gt = A(:,1) > x_range(2);
y_lt = A(:,2) < y_range(1);
y_gt = A(:,2) > y_range(2);
A_out = A;
A_out(x_lt, 1) = x_range(1);
A_out(x_gt, 1) = x_range(2);
A_out(y_lt, 2) = y_range(1);
A_out(y_gt, 2) = y_range(2);
A_out = A_out(x_lt | x_gt | y_lt | y_gt, :);
plot(ah, A_out(:,1), A_out(:,2), 'rx')
This produces a plot like this:
Edit: add callbacks
If you really want to go fancy, you can add callbacks so that outliers are automatically drawn (and removed) when the figure is zoomed. Adding the same functionality for panning is left as an exercise to the reader ;)
mark_outliers.m:
function mark_outliers(fig, ax)
ah = ax.Axes;
lobj = findobj(ah, 'Type', 'Line');
x_range = ah.XLim;
y_range = ah.YLim;
ah.NextPlot = 'add';
for i_l = 1:numel(lobj)
xd = lobj(i_l).XData;
yd = lobj(i_l).YData;
x_lt = xd < x_range(1);
x_gt = xd > x_range(2);
y_lt = yd < y_range(1);
y_gt = yd > y_range(2);
outliers = x_lt | x_gt | y_lt | y_gt;
if any(outliers)
xd_out = xd;
xd_out(x_lt) = x_range(1);
xd_out(x_gt) = x_range(2);
yd_out = yd;
yd_out(y_lt) = y_range(1);
yd_out(y_gt) = y_range(2);
xd_out = xd_out(outliers);
yd_out = yd_out(outliers);
plot(ah, xd_out, yd_out, 'xr', 'Tag', 'Outliers')
end
end
delete_outliers.m:
function delete_outliers(fig, ah)
ah = ah.Axes;
delete(findobj(ah, 'Tag', 'Outliers'));
example.m:
A = randn(20, 2);
fh = figure(1);
clf;
ah = axes;
plot(ah, A(:,1), A(:,2), 'o')
h = zoom(fh);
h.ActionPreCallback=#delete_outliers;
h.ActionPostCallback=#mark_outliers;

Related

How to use togglebutton to turn labels in scatterplot on/off

I tried to create a scatter plot with labels for each point:
Now I would like to give the user of the code the possibility to turn the labels on and off.
So far my code looks like this:
x = rand(1,100); y = rand(1,100); pointsize = 30;
idx = repmat([1 : 10], 1, 10) % I used class memberships here
figure(1)
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
Button = uicontrol('Parent',figure(2),'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',{#pb_call, MDSnorm, ???});
At the end of my script I then tried to define the pb_call function. I tried out several different versions, they all failed.
I have a rough idea what I need to do. Something like:
function [] = pb_call( ??? )
if get(Button, 'Value')
T --> invisible % ???
else
T --> visible % ???
end
end
How can I modify the above to turn labels on or off as desired?
Here is a working example:
x = rand(1,9); y = rand(1,9); pointsize = 30;
idx = repmat(1 : 3, 1, 3); % I used class memberships here
labels = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j'};
h_fig = figure(1); %Create a figure, an keep the handle to the figure.
MDSnorm = scatter(x, y, pointsize, idx, 'filled');
dx = 0.015; dy = 0.015; % displacement so the text does not overlay the data points
T = text(x + dx, y +dy, labels);
colormap( jet ); % in my code I use a specific colormap
%Add a button to the same figure (the figure with the labels).
Button = uicontrol('Parent',h_fig,'Style','toggle','String',...
'labels','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
'callback',#pb_call);
function pb_call(src, event)
%Callback function (executed when button is pressed).
h_fig = src.Parent; %Get handle to the figure (the figure is the parent of the button).
h_axes = findobj(h_fig, 'Type', 'Axes'); %Handle to the axes (the axes is a children of the figure).
h_text = findobj(h_axes, 'Type', 'Text'); %Handle to all Text labels (the axes is the parent of the text labels).
%Decide to turn on or off, according to the visibility of the first text label.
if isequal(h_text(1).Visible, 'on')
set(h_text, 'Visible', 'off'); %Set all labels visibility to off
else
set(h_text, 'Visible', 'on'); %Set all labels visibility to on
end
end
Explanations are in the comments.

create bins based on a range of values for histogram figure

I am doing some analysis and need to produce a histogram plot. I know how to create the standard histogram plot but I need something like the image below, where each point is an interval on the x axis. Each bin is based on a value from x-x for example.
You can use the histogram function, and then set the XTick positions and XTickLabels accordingly. See the comments in the code for explanation.
% random normally distrubuted data
x = 1*randn(1000,1);
edges = -5:1:5;
% create vector with labels (for XTickLabel ... to ...)
labels = [edges(1:end-1); edges(2:end)];
labels = labels(:);
% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges, 'Normalization', 'Probability');
ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% set yticks to percentage
ax.YTickLabel = cellfun(#(a) sprintf('%i%%', (str2double(a)*100)), ax.YTickLabel, 'UniformOutput', false);
% text above bars
bin_props = h.BinCounts/numel(x); % determine probabilities per bin in axis units
bin_centers = ax.XTick(1:end-1); % get the bin centers
txt_heigts = bin_props + 0.01; % put the text slightly above the bar
txt_labels = split(sprintf('%.1f%% ', bin_props*100), ' ');
txt_labels(end) = []; % remove last cell, is empty because of split.
text(ax, bin_centers, txt_heigts, txt_labels, 'HorizontalAlignment', 'center')
% set ylim to fit all text (otherwise text is outside axes)
ylim([0 .4]);
Putting the text at the right location may require some tweaking. Most important is the 'HorizontalAlignment' option, and the distance to the bars. I also used the 'Normalization', 'probability' option from the histogram function, and set the y axis to also show percentages.
I figure you can make the addition below yourself when needed.
When your data can be outside of the defined binedges, you can clip your data, and set the XTickLabels with less than or greater than signs.
% when data can be outside of defined edges
x = 5*randn(1000,1);
xclip = x;
xclip(x >= max(edges)) = max(edges);
xclip(x <= min(edges)) = min(edges);
% plot the histogram
figure();
ax = axes;
h = histogram(xclip, 'BinEdges', edges);
ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(2));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end-1));
You can also set the outer edges to -Inf and Inf, as user2305193 pointed out. Since the outer bins are then much wider (because they actually extend to Inf on the x axis), which you can correct by setting the axis xlim. By the default the XTickLabels will display -Inf to -5.0, which I personally don't like, so I set them to lesser (and equal) than and greater than signs.
step = 1;
edges = -5:step:5; % your defined range
edges_inf = [-Inf edges Inf]; % for histogram
edges_ext = [edges(1)-step edges]; % for the xticks
x = 5*randn(1000,1);
% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges_inf, 'Normalization', 'probability');
labels = [edges_inf(1:end-1); edges_inf(2:end)];
labels = labels(:);
ax.XTick = edges_ext + step/2;
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;
% show all bins with equal width (Inf bins are in fact wider)
xlim([min(edges)-step max(edges)+step])
% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(1));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end));

MATLAB - How to make multiple markers moving simultaneous in 3d plot in MATLAB

I am currently working on a project simulating the movement of two spacecraft and a moon (Phobos) around Mars. A MATLAB tool called SPICE gives me an array with the x, y and z distances and I have used these to plot the orbit which works fine. Now I want to get markers for each of the spacecraft and Phobos to see when they flyby each other. I got the markers working but not at the same time, they run after each other. I found an example on youtube so it must be possible but he has not released the code how to do it (https://www.youtube.com/watch?v=nArR2P0o4r4).
This is the code I have:
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans = makehgtform('translate',[x(i) y(i) z(i)]);
set(ht,'Matrix',trans);
pause(0.001);
end
This will run a nice animated plot of the trajectory of Phobos in time but only Phobos and not simultaneous with MEX and MAVEN (spacecraft from ESA and NASA).
I tried this but does not work:
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans1 = makehgtform('translate',[x(i) y(i) z(i)]);
set(ht,'Matrix',trans1);
trans2 = makehgtform('translate',[a(i) b(i) c(i)]);
set(ht,'Matrix',trans2);
pause(0.001);
end
I also tried merging the arrays so that it plots them each one step after each other but that makes the animation not smooth and is not satisfying for the project.
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
tempx = [position_MEX_Mars(1,:); position_Phobos_Mars(1,:); position_MAVEN_Mars(1,:)];
xt = tempx(:);
tempy = [position_MEX_Mars(2,:); position_Phobos_Mars(2,:); position_MAVEN_Mars(2,:)];
yt = tempy(:);
tempz = [position_MEX_Mars(3,:); position_Phobos_Mars(3,:); position_MAVEN_Mars(3,:)];
zt = tempz(:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans = makehgtform('translate',[xt(i) yt(i) zt(i)]);
set(ht,'Matrix',trans);
pause(0.001);
end
I think I am close but I seem to be missing something and my knowledge of MATLAB is not that great yet. I hope you can help me out.
Cheers Jeroen
Here's a simplified (and not physically correct) example that could perhaps be useful:
t = linspace(0,2,1000); %// time parameter
x1 = 10*cos(2*pi*t+1);
y1 = 5*sin(2*pi*t+1); %// trajectory of object 1
x2 = 2*cos(6*pi*t-2);
y2 = 3*sin(6*pi*t-2); %// trajectory of object 1
plot(x1,y1,'color',[.5 .5 .5]); %// plot trajectory of object 1
hold on
plot(x2,y2,'color',[.5 .5 .5]); %// plot trajectory of object 2
h1 = plot(x1(1),y1(1),'ro'); %// plot initial position of object 1
h2 = plot(x2(1),y2(1),'b*'); %// plot initial position of object 2
axis([-12 12 -12 12]) %// freeze axis size
grid on
for n = 1:numel(t)
set(h1, 'XData', x1(n), 'YData', y1(n)); %// update position of object 2
set(h2, 'XData', x2(n), 'YData', y2(n)); %// update position of object 2
drawnow %// refresh figure
end
The thing that you tries to do is not really as easy as you think. The main issue is that you need to update everything at the same time. This have been implemented in several ways during the years. One way to do this is by using a so called a double buffer.
This means that you have two "surfaces" to paint on. In matlab this is translated to 2 axes (or 2 figures maybe). However, you do only have one axes visible at the same time. This means that you will have time to paint everything on the "hidden surface" before displaying the content. When you are done with everything you need you just switch which surface being visible.
It is possible that this can be done in a simpler way, but I am not familiar with the hgtransfrom functions in matlab.
EDIT
This is an example how it can be done
function test()
fig = figure;
ax1 = axes;
plot(1:10);
ax2 = axes;
plot(1:10,'r');
setVisibility(ax2,'off');
pause(1);
for k = 1:5
setVisibility(ax2,'on');
setVisibility(ax1,'off');
pause(1);
setVisibility(ax1,'on');
setVisibility(ax2,'off');
pause(1);
end
function setVisibility(ax, visibility)
set(ax, 'visible', visibility)
set(findall(ax), 'visible', visibility)

Draw log graph curve on Matlab by clicking?

I'd like to draw a curve on an empty (semilog-y) graph by clicking the points I want it to run through, on the X-Y plane.
Is there a function for this?
edit: I'm trying to do this by obtaining the position of last pointer click -
axis([0 3000 0 1000]);
co=get(gcf, 'CurrentPoint');
It seems to return the cursor position at the time of execution, but it does not change later.
edit2: Here's what works for me. The actual drawing I can do by using the arrays of points collected.
clear
clc
h=plot(0);
grid on;
xlim([0 3000]);
ylim([0 1000]);
datacursormode on;
% Enlarge figure to full screen.
screenSize = get(0,'ScreenSize');
set(gcf, 'units','pixels','outerposition', screenSize);
hold on;
% Print the x,y coordinates - will be in plot coordinates
x=zeros(1,10); y=zeros(1,10);
for p=1:10;
[x(p),y(p)] = ginput(1) ;
% Mark where they clicked with a cross.
plot(x(p),y(p), 'r+', 'MarkerSize', 20, 'LineWidth', 3);
% Print coordinates on the plot.
label = sprintf('(%.1f, %.1f)', x(p), y(p));
text(x(p)+20, y(p), label);
end
Not really, but now there is:
function topLevel
%// parameters
xrange = [0 100];
yrange = [1e-4 1e4];
%// initialize figure, plot
figure, clf, hold on
plot(NaN, NaN);
axis([xrange yrange]);
set(gca, 'YScale', 'log')
t = text(sum(xrange)/2, sum(yrange)/2, ...
'<< Need at least 3 points >>',...
'HorizontalAlignment', 'center');
%// Main loop
xs = []; p = [];
ys = []; P = [];
while true
%// Get new user-input, and collect all of them in a list
[x,y] = ginput(1);
xs = [xs; x]; %#ok<AGROW>
ys = [ys; y]; %#ok<AGROW>
%// Plot the selected points
if ishandle(p)
delete(p); end
p = plot(xs, ys, 'rx');
axis([xrange yrange]);
%// Fit curve through user-injected points
if numel(xs) >= 3
if ishandle(t)
delete(t); end
%// Get parameters of best-fit in a least-squares sense
[A,B,C] = fitExponential(xs,ys);
%// Plot the new curve
xp = linspace(xrange(1), xrange(end), 100);
yp = A + B*exp(C*xp);
if ishandle(P)
delete(P); end
P = plot(xp,yp, 'b');
end
end
%// Fit a model of the form y = A + B·exp(C·x) to data [x,y]
function [A, B, C] = fitExponential(x,y)
options = optimset(...
'maxfunevals', inf);
A = fminsearch(#lsq, 0, options);
[~,B,C] = lsq(A);
function [val, B,C] = lsq(A)
params = [ones(size(x(:))) x(:)] \ log(abs(y-A));
B = exp(params(1));
C = params(2);
val = sum((y - A - B*exp(C*x)).^2);
end
end
end
Note that as always, fitting an exponential curve can be tricky; the square of the difference between model and data is exponentially much greater for higher data values than for lower data values, so there will be a strong bias to fit the higher values better than the lower ones.
I just assumed a simple model and used a simple solution, but this gives a biased curve which might not be "optimal" in the sense that you need it to be. Any decent solution really depends on what you want specifically, and I'll leave that up to you ^_^

how to draw three graphics on the same figure?

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;
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:
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
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
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')