Matlab does not save the upper x-axis label

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

This is strange-looking 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
What are x0,y0? Unless they are arrays or vectors then you're plotting the same point over and over; and if they are, then again you're plotting the same set of data size(filenames,2) times. But then again, if size(filenames,2) is just one, why the loop?
I didn't try to play with the figure sizes; if you try to stretch the sizes to be larger than those default values, you're sorta' on your own to find how much you can stretch them and still have things have suffiient room.
It appears the problem may be compounded by what looks to be a fault in the default spacing calculations when both upper and lower axes are present -- there was a recent thread within last couple weeks where had to do similar sizing experiments to get a figure element to not be clipped when the upper axes was added -- in that case it had to do with the lower axis title getting clipped. Was forced in the end to reduce the height a little as well as adjust the positions.
x0 and y0 are earlier defined as full vectors exported from the files in filenames and they are part of the loop too and thus vary each time. Never mind the detail about it, they are just two lists of related x and y values.
The figure is made this way because I run a long code and then plot different things on different figures, so I need to call which figure I plot what on. Not important for the question, though, so I didn't think it was nesssesary to give all the code.
What I think is the problem is that matlab only saves the window, but my second upper axis label is outside that windue and thus gets cut off when saving. Do you know how to fix that more specificly? :)
/Henriette
>> 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')
Unrecognized function or variable 'lifetimeval'.
>>
Without something we can run to try to duplicate the issue, not much can do except reiterate the advice earlier given that once you manually/programmatically increase sizes of components beyond the boundaries they normally occupy, it's pretty-much trial-and-error to find out how much that can be and not cause such issues. That's on-screen; we can't even tell from what we've got available to us so far whether this is a disply issue or a case only of printing.
If the latter, saveas is pretty limited, you can try the links at the bottom of the documentation in the Tips section with using print to see if any of those ideas helps.
But, if you want somebody here to explore more, they'll need a sample figure they can duplicate to start with.
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')
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 Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 15 Mar 2021

Commented:

dpb
on 17 Mar 2021

Community Treasure Hunt

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

Start Hunting!