5
votes

How to plot something outside the axis with MATLAB? I had like to plot something similar to this figure;

Plot with bar outside axis

Thank you.

3
I don't think this is possible, and unfortunately I don't have a licens a.t.m. Maybe you can do it by modifying the axes properties; mathworks.com/help/techdoc/ref/axes_props.html#Position - Mikael Öhman
I would have expected this to work by setting clipping to off for the barseries, but this doesn't seem to work for me. - Jonas

3 Answers

6
votes

Here is one possible trick by using two axes:

%# plot data as usual
x = randn(1000,1);
[count bin] = hist(x,50);
figure, bar(bin,count,'hist')
hAx1 = gca;

%# create a second axis as copy of first (without its content), 
%# reduce its size, and set limits accordingly
hAx2 = copyobj(hAx1,gcf);
set(hAx2, 'Position',get(hAx1,'Position').*[1 1 1 0.9], ...
    'XLimMode','manual', 'YLimMode','manual', ...
    'YLim',get(hAx1,'YLim').*[1 0.9])
delete(get(hAx2,'Children'))

%# hide first axis, and adjust Z-order
axis(hAx1,'off')
uistack(hAx1,'top')

%# add title and labels
title(hAx2,'Title')
xlabel(hAx2, 'Frequency'), ylabel(hAx2, 'Mag')

and here is the plot before and after:

before_screenshotafter_screenshot

1
votes

You can display one axis with the scale you want, then plot your data on another axis which is invisible and large enough to hold the data you need:

f = figure;

% some fake data
x = 0:20;
y = 23-x;
a_max = 20;
b_max = 23;
a_height = .7;

%% axes you'll see
a = axes('Position', [.1 .1 .8 a_height]);
xlim([0 20]);
ylim([0 20]);

%% axes you'll use
scale = b_max/a_max;
a2 = axes('Position', [.1 .1 .8 scale*a_height]);
p = plot(x, y);
xlim([0 20]);
ylim([0 b_max]);
set(a2, 'Color', 'none', 'Visible', 'off');
1
votes

I had similar problem and I've solved it thanks to this answer. In case of bar series the code is as follows:

[a,b] = hist(randn(1000,1)); % generate random data and histogram
h = bar(b,a); % plot bar series
ylim([0 70]) % set limits
set(get(h,'children'),'clipping','off')% turn off clippings

result