34 views (last 30 days)
Show older comments
Madmad on 29 Aug 2024 at 22:21
Edited: DGM about 7 hours ago
I am saving different maps, I need to compare some specific pixels, but Matlab somehow adds a line at the bottom (3 pixels) for example when I save my image, which kills everything I try to do later. Is there any method to force exportgraphics to use X pixels horizontally and Y vertically (I mean height and width)? Or when I call my figure to be X and Y dimensions? Force it not to add this line. I tried exportgraphics/export_fig they both fail at it.
It is not random, as only some specific files get the line added and always even if I try to replot, I still did not figure out why this happens, my data have the same resolution/grid.
Edit: The issue is what Walter proposed. Sometimes, I have data on the edge of my map, and because in my example I make a plot (set a marker to the area), it can extend beyond the limits, and will require extra pixels from my figure to be displayed. Easy fix is increasing the limit of the map, using smaller markersize, or setting a NaN values to the border of the data. Other fix have still not been provided (imresize does not work as intended). It may be an issue with m_map package pushing the pixels when it needs to, but I have seen this exact same problem for videos, so I guess this is internal to some matlab functions. If someone can reproduce my plot and return the same width/heights with no distortion that would solve the issue.
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
Catalytic on 29 Aug 2024 at 23:21
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249449
We cannot see what you are doing. Attach a figure and run your code on it (but run it here online, not on your own computer).
DGM on 30 Aug 2024 at 0:03
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249464
- captureaxes.m
What is in the figure? Given that you said it's a map, I'm assuming it's not a simple raster image by itself.
Without knowing what's going on with figure generation, I could only guess why it's inconsistent. That's not to say that I expect figure capture to be consistent. I don't have exportgraphics(), so I can't comment on it. The documentation says that there are width and height parameters -- but only for R2024a in MATLAB Online.
Attached is my simple attempt to force the capture of an axes at an explicitly specified geometry. Of course, if the axis limits are not consistent, that won't help. It's also not guaranteed to be exact in every circ*mstance.
Madmad about 20 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249894
Open in MATLAB Online
I added some quick examples:
This returns (I guess it depends on my screen size): 1797x2526 (width / height)
rng('default');
X = 12 + (16-12) * rand(1, 10000);
Y = 25 + (30-25) * rand(1, 10000);
figure('units','normalized','outerposition',[0 0 1 1]);
set(gcf,'color','w')
m_proj('equidistant','lon',[12 16],'lat',[25 30]);
m_plot(X,Y,'.r','markerSize',10)
m_gshhs_h('patch',[0.5 0.5 0.5],'edgecolor','none');
m_grid('XLabel', [],'YLabel',[],'XTickLabel',[],'YTickLabel',[],...
'linestyle','none');
export_fig('ex1.png', '-r300')
close;
1809x2540 (width / height)
X2 = randi([12, 16], 1, 100);
Y2 = randi([25, 30], 1, 100);
figure('units','normalized','outerposition',[0 0 1 1]);
set(gcf,'color','w')
m_proj('equidistant','lon',[12 16],'lat',[25 30]);
m_plot(X2,Y2,'.r','markerSize',20)
m_gshhs_h('patch',[0.5 0.5 0.5],'edgecolor','none');
m_grid('XLabel', [],'YLabel',[],'XTickLabel',[],'YTickLabel',[],...
'linestyle','none');
export_fig('ex2.png', '-r300')
close;
Walter Roberson about 16 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3250139
This appears to use m_map https://www.eoas.ubc.ca/~rich/map.html
Madmad about 15 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3250219
Of course I do, I plot maps
DGM about 7 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3250389
Edited: DGM about 7 hours ago
Open in MATLAB Online
Oh. Your plot markers are not getting clipped to the box. This might be an easy fix:
% ... all the plotting code ...
% set the axes clippingstyle
set(gca,'clippingstyle','rectangle') % not the default
% write the file
fname = 'ex1.png';
export_fig(fname, '-r300')
% check the output geometry
S = imfinfo(fname);
[S.Height S.Width]
I didn't think that would work. See the last example here:
https://www.mathworks.com/help/matlab/creating_plots/clipping-in-plots-and-graphs.html
"Clipping does not affect markers drawn at each data point as long as the data point itself is inside the x- and y-axes limits of the plot. MATLAB displays the entire marker even if it extends slightly outside the boundaries of the axes."
... but apparently that's not true for the '.' marker.
% default clipping
subplot(1,2,1)
p = plot(1:10,'*r','markerSize',50);
axis([1 10 1 10])
% changing the clipping doesn't do anything
subplot(1,2,2)
p = plot(1:10,'*r','markerSize',50);
axis([1 10 1 10])
set(gca,'clippingstyle','rectangle')
% default clipping doesn't affect dot markers either
subplot(1,2,1)
p = plot(1:10,'.r','markerSize',50);
axis([1 10 1 10])
% but dot markers can be clipped
subplot(1,2,2)
p = plot(1:10,'.r','markerSize',50);
axis([1 10 1 10])
set(gca,'clippingstyle','rectangle')
Something tells me that this is something that I've learned before and forgotten. With all the things I've forgotten, it's hard to tell.
Sign in to comment.
Sign in to answer this question.
Answers (2)
Image Analyst on 30 Aug 2024 at 0:57
"I am saving different maps" <== What, like a road map, or an image with different colormaps?
"I save my image" ...with "... exportgraphics." <== If you have an image you should use imwrite, not exportgraphics. It will save exactly what is in the matrix and not add any extra lines or columns.
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Madmad about 20 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249909
I would love to use imwrite, but how to get rid of the white space around my figure? The reason I use epxort_fig/exportgraphics is for that exact reason and I don't know of another way. But sure imwrite is superior most of the time!
Image Analyst about 20 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249939
imwrite saves just the image (matrix). It does not save any extra white space. It does not save any tick marks, labels, titles, etc. Do you want things saved in addition to the matrix alone?
Madmad about 15 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3250214
I mean, basically I want the picture saved as I see it, with max resolution. If I have a label, of course I want it. When I apply it to my fig it does save white space around it, do you have an example that crops data right aorund the axis?
Sign in to comment.
Walter Roberson on 30 Aug 2024 at 2:49
Edited: Walter Roberson on 30 Aug 2024 at 3:57
You can getframe() and imresize() to a fixed size
The results are not likely to be satisfactory. Yes, you would get out the same sized image each time -- but the images would not generally be aligned.
It turns out that the exact size the MATLAB choses to render an axes depends upon the tick labels. The right-most tick label might be shifted up to 2 pixels left or up to 3 pixels right in order to have the labels be positioned "nicely". This changes the axes size. And it depends on exactly how many pixels are needed to render the tick labels. Remember that tick labels are typically proportionate fonts, so it isn't enough to make sure that the labels are always the same number of characters. 11.75 needs a different number of pixels than 12.75 does, for example.
To ensure that the axes is a consistent size you would need to use constant x and y tick labels and constant x and y labels and constant x and y data span and constant titles.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
DGM on 30 Aug 2024 at 3:27
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2148864-force-saved-figure-to-use-a-specific-number-of-pixels#comment_3249569
I never realized that the tick labels were part of the equation, but it makes sense that it would appear so unpredictable. I might have to dig into that.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsPrinting and Saving
Find more on Printing and Saving 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office