Axis label and tick label offset scaling control?

Hello,
In my work I have the need to export figures to image files with greatly elongated lengths. I have been attempting to accomplish this by setting the 'paperposition' property and exporting the figure with the saveas function.
I have found, unexpectedly, that the length of the figure affects the offset of axis labels and tick labels (both x and y axis) from the displayed axes. By the time I'm at the length I need the axis and tick labels may be clipped or entirely moved off of the exported page. The code and figures below show some examples of this behaviour.
I would like to know if there is a way to prevent the axis labels and axis tick labels from drifting away from the axis as I elongate the figure output. Ideally I'd like to be able to directly control the location of the axis labels and axis tick labels relative to the axes if that is possible.
So far the only workaround I've found is to decrease the size of the axes on the figure but this results in a lot of wasted whitespace in the output image to accomodate the growing gaps between the labels and the axes and I don't consider it an acceptable solution.
I am using MATLAB Version: 9.3.0.713579 (R2017b). Any help is greatly appreciated. Thank you!
% Generate sample figure
figure;
subplot(121);
plot(cumsum(randn(1e4,2)),1:1e4);
set(gca,'ytick',0:100:1e4,'ticklength',[0,0]);
grid on;box on;
xlabel('X Axis Label');
ylabel('Y Axis Label');
subplot(122);
plot(cumsum(randn(1e4,2)),1:1e4);
set(gca,'ytick',0:100:1e4,'ticklength',[0,0]);
grid on;box on;
xlabel('X Axis Label');
ylabel('Y Axis Label');
% Output Image Length = 11 inches
% Output looks reasonable.
set(gcf,'paperposition',[0,0,8.5,11]);
saveas(gcf,'fig1.png');
% Output Image Length = 400 inches
% Left plot y-axis label and y-axis tick labels off of page.
% Right plot y-axis label and y-axis tick labels overlap left plot.
% Large offset between axes and x-axis labels as well.
set(gcf,'paperposition',[0,0,8.5,400]);
saveas(gcf,'fig2.png');
FIG 1. Excerpts from "fig1.png" generated by sample code above. Offsets of axis ticks and axis labels from axes look reasonable.
FIG 2. Excerpts from "fig2.png" generated by sample code above. Offsets of axis ticks and axis labels from axes are too large.

5 Comments

I was going to say try to also set the "Position" property to match the PaperPosition dimensions...but when I just tried it, matlab wouldn't let me set the Position value...maybe it isn't letting me draw something bigger than my screen?
UPDATE:
I tried setting the 'activepositionproperty' property of each axes to 'outerposition' before exporting with saveas() (code snippet below). This prevents the left y-axis label text from running off of the page and prevents the right y-axis label text from overlapping the left axes, however, it shrinks the width of both axes and leads to an unacceptable (and seemingly uncontrollable) amount of whitespace between the y-axis labels and the axes (see fig4.png output from code below (also attached)).
I've also tried experimenting with the undocumented 'looseinset' axes property (http://undocumentedmatlab.com/articles/axes-looseinset-property) but still have not been able to achieve the result I'm after (an elongated plot image without excessive spacing between the axis labels, tick labels, and axes).
% Generate sample figure
figure;
subplot(121);
plot(cumsum(randn(1e4,2)),1:1e4);
set(gca,'ytick',0:100:1e4,'ticklength',[0,0]);
set(gca,'activepositionproperty','outerposition');
grid on;box on;
xlabel('X Axis 1 Label');
ylabel('Y Axis 1 Label');
subplot(122);
plot(cumsum(randn(1e4,2)),1:1e4);
set(gca,'ytick',0:100:1e4,'ticklength',[0,0]);
set(gca,'activepositionproperty','outerposition');
grid on;box on;
xlabel('X Axis 2 Label');
ylabel('Y Axis 2 Label');
% Output Image Length = 11 inches
% Output looks reasonable.
set(gcf,'paperposition',[0,0,8.5,11]);
saveas(gcf,'fig3.png');
% Output Image Length = 400 inches
% Growth of spacing between the y-axis label text and the axes causes the
% width of the axes to shrink by an unacceptable amount.
set(gcf,'paperposition',[0,0,8.5,400]);
saveas(gcf,'fig4.png');
i haven't tested or anything, but does the "print" command behave any differently?
JAL -- Thanks very much for your suggestions. I get the same results that I show above when I use the print() function to export images. I'm starting to think I may have to redraw the tick marks and labels myself using text() or uicontrol() calls if I want to gain control over their placement relative to the axes. I'd love to avoid this, however, if at all possible.
How about setting dimensions of the axes itself? You can set its units to inches and try setting some rational margins
ax(1) = subplot(121);
ax(2) = subplot(122);
set(ax,"Units","inches")
% set width and height
ax(1).Position(3:4) = [4 , 400 - 2];
ax(2).Position(3:4) = [4 , 400 - 2];
% set bottom margin
ax(1).Position(2) = 1; % or whatever
ax(2).Position(2) = 1; % or whatever
% set horizontal positions
ax(1).Position(1) = 0.3; % or whatever
ax(2).Position(1) = 4.1; % or whatever

Sign in to comment.

Answers (0)

Asked:

on 18 Aug 2020

Commented:

on 24 Aug 2020

Community Treasure Hunt

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

Start Hunting!