Matlab does not save the upper x-axis label

5 views (last 30 days)
Henriette Larsen
Henriette Larsen on 15 Mar 2021
Commented: dpb on 17 Mar 2021
So, I have a figure with two x-axes - a lower and an upper one to show the time in two units. But the label of the upper one does not get saved.
I have tried to play around with the outer position and if I set height at 0.95 or 1.0 (normalized), I do get both axis labels saved. BUT I'd very much like the figure to be longest horisontally i.e. outer positions like [0, 0.05 0.7 0.6] or something not too far from that.
Any idea how to do that? please ;)
- Henriette
My code:
for i = 1:size(filenames,2)
figure(1)
hold on
p1(i) = plot(x0,y0,'.','color',colors{i},'MarkerSize',8);
pfit(i) = plot(x0,fexp(K,x0),'-','Color','k','Linewidth',1.5);
end
figure(1);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.1, 0.7, 0.6])
grid on
set(0,'defaultlinelinewidth',1)
set(0,'defaultpatchlinewidth',1)
set(gca,'Fontsize',11)
ax = gca;
ax.LineWidth = 1;
xlim([0 30])
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position',[0.1300 0.1100 0.7750 0.8150],'XAxisLocation','top',...
'YAxisLocation','right','Color','none','XTick',...
[0 1 2 3 4 5 6 7 8]*lifetimeval,'YTick',[]);
grid on;
set(0,'defaultlinelinewidth',1)
set(0,'defaultpatchlinewidth',1)
set(ax2,'Fontsize',11)
ax2.LineWidth = 1;
set(ax2,'xcolor','k')
set(ax2,'XTickLabel',{'0','1','2','3','4','5','6','7','8'})
ylabel(ax1,'Intensity [V]','Interpreter','latex')
xlabel(ax1,'Time [ms]','Interpreter','latex')
xlabel(ax2,'Time [lifetimes]','Interpreter','latex')
lgd1 = legend(ax2,[p1(7),p1(6),p1(5),p1(4),p1(3),p1(2),p1(1),pfit(1)],...
{'Gain: 30 dB','Gain: 25 dB','Gain: 20 dB','Gain: 15 dB','Gain: 10 dB','Gain: 5 dB','Gain: 0 dB','Exponential fits'},...
'Location','NorthEast');
set(lgd1, 'Box', 'on', 'Color', 'w')
hold off
saveas(gca,figname1,'epsc')
saveas(gca,figname1,'jpg')
  5 Comments
Henriette Larsen
Henriette Larsen on 16 Mar 2021
Edited: dpb on 16 Mar 2021
Here is a code that can be copy pasted into your own matlab program and tested. It gives the same problem as decribed i.e. the upper x-axis is excluded from the plot window and not saved. Sorry for the confussion above. Thanks :)
x = linspace(0,20,100);
y = 10*exp(-x/3)+rand(1,100)-0.5;
k0 = [10 -0.3];
fexp = @(beta,x) beta(1).*exp(beta(2).*x);
[k] = nlinfit(x,y,fexp,k0);
lifetime = -1/k(2);
figure
hold on
p1 = plot(x,y,'*');
pfit = plot(x,fexp(k,x),'k-');
grid on
ax1 = gca;
xlim([0 20])
ax1_pos = ax1.Position;
ax2 = axes('Position',ax1_pos,'XAxisLocation','top',...
'YAxisLocation','right','Color','none','XTick',...
[0 1 2 3 4 5 6]*lifetime,'YTick',[]);
grid on;
set(ax2,'XTickLabel',{'0','1','2','3','4','5','6'})
xlim([0 20])
ylabel(ax1,'Intensity [V]','Interpreter','latex')
xlabel(ax1,'Time [ms]','Interpreter','latex')
xlabel(ax2,'Time [lifetimes]','Interpreter','latex')
lh = legend(ax2,[p1,pfit],{'Data','Exponential fit'},'Location','NorthEast');
set(lh, 'Box', 'on', 'Color', 'w')
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.1, 0.7, 0.6])
hold off
saveas(gca,'test','jpg')
dpb
dpb on 17 Mar 2021
Using the File PrintAs and landscape mode, a saved .jpg looks like the above...it (print) appears smart enough to fit to page. Experiment there with programmatic control.

Sign in to comment.

Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!