Can't change xtick font size in Matlab subplot - matlab

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

Related

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

Missing vertical tick when using boxplot in matlab 2014a

I am using boxplot to draw my data using matlab 2014a. I used below code to plot my data and rotate my xlabel. However, it lost the vertical tick in figure. Could you see my expected figure (right side) and help me to add that tick in below code. Thanks
figure(1);
set(gcf,'color','w');
hold on;
rotation = 25;
fontSize = 12;
str={'Test 1','Test 2'};
x1 = normrnd(5,1,100,1);
x2 = normrnd(6,1,100,1);
ax1=subplot(1,2,1); boxplot([x1, x2]); title('This is result for above code','FontSize', fontSize);
% ylim([0 1]);
text_h = findobj(gca, 'Type', 'text');
for cnt = 1:length(text_h)
set(text_h(cnt), 'FontSize', fontSize,...
'Rotation', rotation, ...
'String', str{length(str)-cnt+1}, ...
'HorizontalAlignment', 'right')
end
set(ax1, 'fontsize', fontSize);
set(findobj(ax1,'Type','text'),'FontSize', fontSize);
ylabel('Temp')
boxplot doesn't automatically create xticks in Matlab R2014a and older versions (it does in R2015a, and probably in R2014b too).
You can add the xticks manually as follows:
set(ax1, 'xtick', [1 2])

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.

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

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