Matlab does not save the upper x-axis label
Show older comments
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
dpb
on 15 Mar 2021
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.
Henriette Larsen
on 15 Mar 2021
Edited: Henriette Larsen
on 15 Mar 2021
dpb
on 15 Mar 2021
>> 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.
Henriette Larsen
on 16 Mar 2021
Edited: dpb
on 16 Mar 2021
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.
Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!