Also, the Y dimensions in the saved plot are totally wrong. I need the "plot box" to fill the image from top to bottom.
Specify "plot box" position for tiled layout
19 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am creating a png file from a plot and I need to specify the exact size of the figure and the plot area in pixels. I am using a tiled layout and set(gcf, 'Position', ...) doesn't seem to work.
Here a test.
The function figSave2 is a modified version of a function I found here in the Community. I added the 'InnerPosition' argument.
X = 1:100;
Y = 1:100;
figure;
tiledlayout(3,1,'TileSpacing','None');
for i=1:3
nexttile(i);
plot(X,Y,'b-');
end
figSave2(gcf,'testFileSave2.png',[2400 900],[166 0 2100 900]);
close( gcf );
function figSave2(fig,file,rez,plotbox,txt,bak)
%Save figure as image, with custom resolutions and text scaling.
% figsave(fig,file,rez,plotbox,txt,bak)
%
%Example:
% clf,text(0.1,0.5,{'This text should be';'50 pixels high';'and the image';'900W x 600H pix'},'FontSize',50)
% figSave(gcf,'Figure.jpg',[900 600],[100 100 700 400])
%
%
% https://www.mathworks.com/matlabcentral/answers/272767-how-to-set-exact-figure-size-in-pixels
if nargin<1 || isempty(fig), fig = gcf; end %figure handle
if nargin<2 || isempty(file), file = 'Figure.jpg'; end %output file name
if nargin<3 || isempty(rez), rez = [900 600]; end %resolution [horizontal vertical]
if nargin<4 || isempty(plotbox), plotbox = [100 100 700 400]; end %resolution [horizontal vertical]
if nargin<5 || isempty(txt), txt = 1; end %text scale factor
if nargin<6 || isempty(bak), bak = 0; end %dont preserve background colour
set(fig,'PaperPosition',[0 0 rez/(100*txt)],'InnerPosition',plotbox/(100*txt),'PaperUnits','inches'); %set paper size (does not affect display)
if bak
set(fig,'InvertHardcopy','off'); %preserve background colour
end
imwrite(print(gcf,'-RGBImage',['-r' num2str(100*txt,'%f')]),file) %print RGB image to file (slow)
end
The resulting plot does not have the specified offset of 166 pixels to the Y axes, it's more like 140.
How can I set the postion of the "plot box" that is made up from the three tiled plots?
Risposta accettata
Adam Danz
il 8 Ott 2024
> How can I set the postion of the "plot box" that is made up from the three tiled plots?
The position property for axes in a TiledChartLayout cannot be set. However, since want to control the global position that encompasses all three axes, you could set the position of the TiledChartLayout and use the full area of the tiled layout by setting padding to tight. However, this won't frame the global plot box area since the x tick and y tick labels will be within the frame.
tcl = tiledlayout(3,1,'units','pixels','position', [50 50 300 300],'padding','tight','TileSpacing','none');
ax(1) = nexttile(tcl);
ax(2) = nexttile(tcl);
ax(3) = nexttile(tcl);
box(ax,'on')
Show the position of the tiled layout with a red dashed line.
annotation('rectangle',...
'units',tcl.Units,...
'Position',tcl.Position,...
'LineWidth',2,...
'Color','r',...
'lineStyle','--')
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!