Axis label and tick label offset scaling control?
조회 수: 26 (최근 30일)
이전 댓글 표시
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
J. Alex Lee
2020년 8월 24일
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Printing and Saving에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!