how to remove the gap between subplots and around [duplicate] - matlab

This question already has answers here:
How to control the margin size around subplots?
(5 answers)
Closed 6 years ago.
I am plotting two subplots (2x1) in one figure. I would like to remove all the spacing between two subplots and remove the xlable and xlabel ticks for the top subplot too. Also, I am trying to remove all the spacing outside the subplot. I try
set(gca, 'LooseInset', get(gca,'TightInset'))
But it doesn't work. Now I am removing those margins and labels manually, I have 60 figures need to be handled and doing all those manually is time consuming. Any better way to do it? Thanks.
I also try the subtightplot, it helps to reduce all the margins but the xlabel and ylabel are also cut
margins=[0 0];
t = 0:0.01:10;
y1 = sin(t);
y2 = cos(t);
h1 = subtightplot(2,1,1, margins);
plot(t, y1);
ystr = {'sin(x)','(dimensionless)'}
hy1 = ylabel(ystr);
set(gca, 'fontsize', 14);
set(hy1, 'fontsize', 14);
set(gca, 'XTickLabel', [],'XTick',[])
h2 = subtightplot(2,1,2,margins);
plot(t, y2, 'r-o');
hx2=xlabel('frequency');
hy2=ylabel('amplitude');
set(gca, 'fontsize', 14);
set(hx2, 'fontsize', 14);
set(hy2, 'fontsize', 14);
I also try the subplot_tight but it is even worse

you can use subplot_tight or subtightplot from the FEX.
to remove all x-tick and labels use:
set(gca, 'XTickLabel', [],'XTick',[])
in the appropriate subplot...
Edit:
Since you do want to include the labels etc, you can so it using the position handle in axes:
t = 0:0.01:10;
y1 = sin(t);
y2 = cos(t);
left= 0.15;
bottom1=0.5;
bottom2=0.05;
width=0.8;
height=0.45; % which is also bottom1-bottom2
axes('Position',[left bottom1 width height]);
plot(t, y1);
ystr = {'sin(x)','(dimensionless)'}
hy1 = ylabel(ystr);
set(gca, 'fontsize', 14);
set(hy1, 'fontsize', 14);
set(gca, 'XTickLabel', [],'XTick',[])
axes('Position',[left bottom2 width height])
plot(t, y2, 'r-o');
hx2=xlabel('frequency');
hy2=ylabel('amplitude');
set(gca, 'fontsize', 14);
set(hx2, 'fontsize', 14);
set(hy2, 'fontsize', 14);

Related

grouped bar chart with 2 y-axes is displayed as stacked bar

I would like to plot the grouped bar chart for the comparison of three methods. I tried the following code and it is displayed as stacked bar chart.can you please help
dice =[0, 3, 5];
no_of_region=[42, 12, 5];
figure;
bar(dice',.2,'grouped','FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
ylabel('Dice Similarity index')
yyaxis right
bar(no_of_region, .2,'grouped','EdgeColor', 'r', 'LineWidth', 2);
legend('Dice Similarity Index','Number of regions')
legend('Location','northwest')
XTickLabel={'a' ; 'b';'c'};
XTick=[1 2 3]
set(gca, 'XTick',XTick);
set(gca, 'XTickLabel', XTickLabel);
set(gca, 'XTickLabelRotation', 45);
xlabel('Different Methods', 'Fontsize', 16)
ylabel('Number of Regions', 'Fontsize', 16)
title('Comparison of Algorithms', 'fontsize', 20);
set(gcf,'color','w');
OUTPUT I GOT AS:
One easy solution is to shift the bars left and right by half of their width (or more if you would like space between them). Here is the new code, which is also cleaned a bit stylistically:
% parameters
dice = [0, 3, 5];
no_of_region = [42, 12, 5];
X = [1, 2, 3]; % common x values for plot
bar_width = 0.2; % width of bars
% initialize figure
figure;
% left plot
bar(X.' - bar_width/2, dice', bar_width, 'grouped', ...
'FaceColor', [0 .5 .5], ...
'EdgeColor', [0 .9 .9], ...
'LineWidth', 1.5);
ylabel('Dice Similarity index');
% right plot
yyaxis right;
bar(X.' + bar_width/2, no_of_region, bar_width, 'grouped', ...
'EdgeColor', 'r', ...
'LineWidth', 2);
% plot formatting
legend({'Dice Similarity Index', 'Number of regions'}, ...
'Location', 'northwest');
XTickLabel = {'a' ; 'b'; 'c'};
XTick = X;
set(gca, ...
'XTick', XTick, ...
'XTickLabel', XTickLabel, ...
'XTickLabelRotation', 45);
xlabel('Different Methods', 'FontSize', 16);
ylabel('Number of Regions', 'FontSize', 16);
title('Comparison of Algorithms', 'FontSize', 20);
set(gcf, 'color', 'w');
Depending on desired behavior, you also might consider replacing figure; by clf;, which clears the existing figure or opens a new figure window if there is not already one open.

Multiple y axes in MATLAB (axis alignment issue when printing)

Here is a sample of a strange problem. I'd like to plot curves with multiple y axes and I use a fairly common method in MATLAB.
function plot_test()
clear;
savefile = 1;
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]);
hold on;
box on;
x = 0:1:10;
h1 = plot(x, x.^2 , 'r', 'LineWidth', 2.5);
%Axis label
xlabel('XLabel', 'FontSize', 20, 'Interpreter', 'latex');
ylabel('YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(gca, 'FontSize', 20, 'LineWidth', 3);
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','none','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
box on;
h2 = plot(x, x, 'b', 'Parent', ax2, 'LineWidth', 2.5);
ylabel('Second YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(gca, 'FontSize', 20, 'LineWidth', 3);
hl=legend([h1 h2],{'First Line','Second Line'});
set(hl,'FontSize',15,'Location','Northwest', 'Orientation','Vertical')
%Save pdf
if savefile
% Backup previous settings
prePaperType = get(gcf,'PaperType');
prePaperUnits = get(gcf,'PaperUnits');
preUnits = get(gcf,'Units');
prePaperPosition = get(gcf,'PaperPosition');
prePaperSize = get(gcf,'PaperSize');
% Make changing paper type possible
set(gcf,'PaperType','<custom>');
% Set units to all be the same
set(gcf,'PaperUnits','inches');
set(gcf,'Units','inches');
% Save the pdf
print -dpdf Test.pdf;
% Restore the previous settings
set(gcf,'PaperType',prePaperType);
set(gcf,'PaperUnits',prePaperUnits);
set(gcf,'Units',preUnits);
set(gcf,'PaperPosition',prePaperPosition);
set(gcf,'PaperSize',prePaperSize);
end
The objective is to print a PDF of the figure and save it in the same folder as Test.pdf. This is accomplished but the axes are misaligned. On my Windows machine it looks horrible while on a Mac it looks almost okay (but if you look closely, the y-axes are indeed misaligned at the bottom).
This only happens when I use a second axis. Without that, all this runs perfectly. Any idea why?
Okay, so I found a way: The trick is to use plotyy. Sample code below
function plot_test2()
clear;
savefile = 1;
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]);
hold on;
box on;
x=(0:1:10);
y1=x;
y2=x.^2;
[hAx, hLine1, hLine2] = plotyy(x,y1,x,y2);
%Axis label
xlabel(hAx(1),'XLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(1),'YLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(2),'Second YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(hAx,{'ycolor'},{'k';'k'})
set(hAx,{'FontSize'},{20;20}, {'LineWidth'}, {3;3})
set(hLine1,'LineWidth', 3)
set(hLine2,'LineWidth', 3)
set(hLine1,'Color', 'r')
set(hLine2,'Color', 'b')
hl=legend([hLine1 hLine2],{'First Line','Second Line'});
set(hl,'FontSize',15,'Location','Northwest', 'Orientation','Vertical')
%Save pdf
if savefile
% Backup previous settings
prePaperType = get(gcf,'PaperType');
prePaperUnits = get(gcf,'PaperUnits');
preUnits = get(gcf,'Units');
prePaperPosition = get(gcf,'PaperPosition');
prePaperSize = get(gcf,'PaperSize');
% Make changing paper type possible
set(gcf,'PaperType','<custom>');
% Set units to all be the same
set(gcf,'PaperUnits','inches');
set(gcf,'Units','inches');
% Save the pdf
print -dpdf Test.pdf;
% Restore the previous settings
set(gcf,'PaperType',prePaperType);
set(gcf,'PaperUnits',prePaperUnits);
set(gcf,'Units',preUnits);
set(gcf,'PaperPosition',prePaperPosition);
set(gcf,'PaperSize',prePaperSize);
end

Can't change xtick font size in Matlab subplot

I'd like to change the font size of the x & y tick labels, but have only been able to change the size of the y tick labels.
Below is the code that changes only the font size of the y tick labels:
figure(1);
for z=1:length(percentsolar)
for i=1:h
percentimprovement4(:,i) = percentimprovement2(1,:,i,z,1);
end
ax(z) = subplot(3,2,z);
boxplot(percentimprovement4);
set(ax(z), 'fontsize', 6);
ylabel('% improvement', 'fontsize',8,'fontweight', 'bold');
xlabel('Hour of the day', 'fontsize', 8,'fontweight', 'bold');
title(['PF improvement for ', num2str(percentsolar(z)),'% solar penetration'], 'fontsize', 10 ,'fontweight', 'bold');
clear percentimprovement4
end
linkaxes(ax);
saveas(gcf,'Boxplotshourly.jpg');
As written here:
boxplot() uses the default axes labeling for the Y axes, but for the X
axes, it uses text() to put the labels in place and it does not
grab the axes FontSize when it does so.
Thus, in addition to set(ax(z), 'fontsize', 6); you should also use set(findobj(ax(z),'Type','text'),'FontSize', 6);. For example,
figure(1);
percentsolar = zeros(1,6);
z = 6;
ax = zeros(0, length(percentsolar));
for z = 1:length(percentsolar)
ax(z) = subplot(3,2, z);
x1 = normrnd(5,1,100,1);
x2 = normrnd(6,1,100,1);
boxplot([x1, x2]);
set(ax(z), 'fontsize', 6);
set(findobj(ax(z),'Type','text'),'FontSize', 6);
ylabel('% improvement', 'fontsize',8,'fontweight', 'bold');
xlabel('Hour of the day', 'fontsize', 8,'fontweight', 'bold');
end

Loop over subplots

Somehow, the title, xlabel, ylabel, ylim and xlim does not stay fixed in the script below, how do I fix this?
x = 0:0.01:1;
figure
p1 = subplot(2,1,1);
xlabel(p1, '$x$','Interpreter','LaTex', 'Fontsize', 16);
ylabel(p1, '$\sin(x+t)$','Interpreter','LaTex', 'Fontsize', 16);
title(p1, 'Sine','Interpreter','LaTex', 'Fontsize', 20);
ylim(p1, [-1 2])
xlim(p1, [0 1])
p2 = subplot(2,1,2);
xlabel(p2, '$x$','Interpreter','LaTex', 'Fontsize', 16);
ylabel(p2, '$\cos(x+t)$','Interpreter','LaTex', 'Fontsize', 16);
title(p2, 'Cosine','Interpreter','LaTex', 'Fontsize', 20);
ylim(p2, [-2 1])
xlim(p2, [0 1])
for t = 0:0.1:2
plot(p1, x, sin(x+t))
plot(p2, x, cos(x+t))
pause(0.1)
end
By default, since you haven't got hold on or hold all, when you replot in the loop everything is reset. Since you only want to change one set of values in each graph, you can use set rather than plot here to get around the issue.
First, after each subplot is called, plot the initial graph and take a handle for it:
p1 = subplot(2,1,1);
h1 = plot(p1,x,sin(x);
Then continue on setting up the labels, etc, as you already do.
In the loop, to replace the data in your existing graph:
for t = 0.1:0.1:2
set(h1,'Ydata',sin(x+t))
set(h2,'Ydata',cos(x+t))
pause(0.1)
end

How to get arrows on axes in MATLAB plot?

I want to plot something like this:
x = 0:0.01:10;
f = #(x) 50* 1.6.^(-x-5);
g = #(x) 50* 1.6.^(+x-10);
plot(x, f(x));
hold on
plot(x, g(x));
I can't manage to get axes similar to the ones in this figure:
I know I can remove the top and right lines like in this question, but I don't know how to get the arrows on the edges.
I don't need the additional annotations, but I would like to remove the ticks on the axes. I know how to do this when the axes are "normal", but I'm not sure if it must be done in another way when the axes are already manipulated.
Does anyone know how to do this?
Well, don't say I didn't warn you :)
% Some bogus functions
f = #(x) 50* 1.6.^(-x-5);
g = #(x) 50* 1.6.^(+x-10);
% Point where they meet
xE = 2.5;
yE = f(xE);
% Plot the bogus functions
figure(1), clf, hold on
x = 0:0.2:5;
plot(x,f(x),'r', x,g(x),'b', 'linewidth', 2)
% get rid of standard axes decorations
set(gca, 'Xtick', [], 'Ytick', [], 'box', 'off')
% Fix the axes sizes
axis([0 5 0 5])
% the equilibrium point
plot(xE, yE, 'k.', 'markersize', 20)
% the dashed lines
line([xE 0; xE xE], [0 yE; yE yE], 'linestyle', '--', 'color', 'k')
% the arrows
xO = 0.2;
yO = 0.1;
patch(...
[5-xO -yO; 5-xO +yO; 5.0 0.0], ...
[yO 5-xO; -yO 5-xO; 0 5], 'k', 'clipping', 'off')
% the squishy wiggly line pointing to the "equilibrium" text
h = #(x)0.5*(x+0.2) + 0.1*sin((x+0.2)*14);
x = 2.7:0.01:3.5;
plot(x, h(x), 'k', 'linewidth', 2)
% the static texts
text(xE-yO, -0.2, 'Q^*', 'fontweight', 'bold')
text(-2*yO, yE, 'P^*', 'fontweight', 'bold')
text(-2*yO, 4, 'Price', 'rotation', 90, 'fontsize', 14)
text( 4, -0.2, 'Quantity', 'fontsize', 14)
text( .5, 4.2, 'Demand', 'fontsize', 14, 'rotation', -55)
text( 4.0, 3.3, 'Supply', 'fontsize', 14, 'rotation', +55)
text( 3.6, 2.1, 'Equilibrium', 'fontsize', 14)
Result:
The symbolic math toolbox has provisions for making these arrows, but without that toolbox you are stuck with drawing the arrows yourself. The following code should be useful for this purpose:
% determine position of the axes
axp = get(gca,'Position');
% determine startpoint and endpoint for the arrows
xs=axp(1);
xe=axp(1)+axp(3)+0.04;
ys=axp(2);
ye=axp(2)+axp(4)+0.05;
% make the arrows
annotation('arrow', [xs xe],[ys ys]);
annotation('arrow', [xs xs],[ys ye]);
% remove old box and axes
box off
set(gca,'YTick',[])
set(gca,'XTick',[])
set(gca,'YColor',get(gca,'Color'))
set(gca,'XColor',get(gca,'Color'))
The only drawback is that for some figure window sizes you will have a 1-pixel white border below the arrows, and setting the LineWidth property of the axes to a ridiculous small value does not help.
But for printing, the small white border should not be relevant.