How to plot subplots with zero gaps and x and y labels at the sides?

I would like to plot lots of histograms. They all have the same size (xlim, ylim), so equal size plots. How to put them next to each other with zero gap? I would like to use the hist function to generate the histograms. Morover I dont want any tics and lebels, just at the top of each column, and at the left side of each rows (a single string is enough like: 'col1' 'row2' etc.)

 Risposta accettata

Hi,
One way could be to plot all the histograms using "subplot". You can then remove the X and Y ticks using command:
set(gca,'xtick',[])
set(gca,'ytick',[])
Then click on Tools -> Edit Plot and modify the location of the plot by dragging them. You can also add titles and labels using Insert option. Then generate code from the File option.
If this is not feasible, you can try to modify the location of the subplots manually using "pos" property of subplot.
Suppose, we have a subplot like this:
figure
h1 = subplot(2,2,1)
hist(4)
set(gca,'xtick',[])
set(gca,'ytick',[])
You can now modify the position of the subplot using:
p1 = get(h1, 'pos');
p1(3) = p1(3) + 0.05;
p1(2) = p1(2) - 0.06;
set(h1, 'pos', p1);
This can be done for all the subplots till the gap between each of them reduces.
HTH

4 Commenti

Yes, that's right. But when you're packing the axes together this tight, and you have to start overriding the Position property, there's really not a lot of advantage to using subplot. It's probably going to get in your way more than it will help.
I might consider using the axes command directly. Something like this:
% # of rows & columns
nr = 2;
nc = 2;
% Margins
left = .1;
right = .01;
top = .1;
bottom = .02;
% Size of cells
w = (1-left-right)/nc;
h = (1-top-bottom)/nr;
for r=1:nr
for c=1:nc
% Position of this cell
pos = [left+(c-1)*w, bottom+(r-1)*h, w, h];
% Create axes
a = axes('Position',pos,'LooseInset',[0 0 0 0]);
% Add histogram (replace with hist in earlier versions)
histogram(randn(1,100))
% customize axes
set(a,'XTick',[],'YTick',[],'XLim',[-4 4],'YLim',[0 40])
if c==1
set(a,'YAxisLocation','left');
ylabel(['Row ', num2str(r)])
end
if r==nr
set(a,'XAxisLocation','top');
xlabel(['Column ', num2str(c)])
end
end
end
Mr M.
Mr M. il 5 Ott 2015
Modificato: Mr M. il 5 Ott 2015
Thank you very much! The concept is exactly the same what I want, and it works for me, and this is a very general solution! I think it would be very useful for others also. Thanks again. How can I give you my vote?
However I want to ask, what is the purpose of 'LooseInset', because without that, I get the same result!
However I want to ask, what is the purpose of 'LooseInset', because without that, I get the same result!
Oops, I should have deleted that. You're right, it's not required.
Mr M.
Mr M. il 5 Ott 2015
Modificato: Mr M. il 5 Ott 2015
Another question: how to set margin to be the same size as the text, so labels fit exactly into the left and top margins.
Another very little thing: at the corners of the subplots, the axes join to each other in a very ugly way (if you zoom in). Maybe the problem is the endpoint style of the lines. It is possible to make rounded endpoints or something like that?
And a third problem: I want a global title. So I tried to put something like this at the begining:
title_height = 0.05; figure('units','normalized','outerposition',[0 0 1 1]); axes_position = [0,0,1,1-title_height]; plot_axes = axes('Position',axes_position); set(plot_axes,'XTick',[],'YTick',[],'xcolor','none'); title('Title');
The problem is that axes are visible in pdf output. I tried 'color','white' and 'xcolor','white' and 'box','off', 'linewidth',0 etc. but none of them is working!

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 27 Set 2015

Modificato:

il 5 Ott 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by