Matlab does not save the upper x-axis label

조회 수: 5 (최근 30일)
Henriette Larsen
Henriette Larsen 2021년 3월 15일
댓글: dpb 2021년 3월 17일
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
Henriette Larsen
Henriette Larsen 2021년 3월 16일
편집: dpb 2021년 3월 16일
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 2021년 3월 17일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by