Axis label and tick label offset scaling control?
21 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/347897/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/347900/image.png)
FIG 2. Excerpts from "fig2.png" generated by sample code above. Offsets of axis ticks and axis labels from axes are too large.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/347903/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/347906/image.png)
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 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!