Create a figure -exactly- a certain size
5 views (last 30 days)
Show older comments
Consider the following code:
N = 2000;
[X,Y] = meshgrid(linspace(0,2*pi,N),linspace(0,2*pi,N));
Z = sin(X+Y);
p = pcolor(X,Y,Z)
Effectively, this colors each of the NxN elements of the matrix Z a given color. If you're running this, change N to something smaller.
I want to generate a .png figure that is exactly N x N pixels, each pixel representing one of the elements of Z.
How would I do that?
Additional notes:
This almost does it.
set(p, 'EdgeColor', 'none');
set(gca, 'Visible', 'off')
set(gcf, 'Units', 'pixels', [100 100 N+200 N+200]);
set(gca, 'Units', 'pixels', [50 50 N N]);
export_fig 'test' -png
There are two problems with this code:
- If N is large, then the figure is the incorrect dimension. For example, if N = 2000, then it seems that the figure gets expanded only until the height of your screen is reached. I need to be able to create arbitrarily sized images. The screen display is not important.
- The resultant figures are N+2 times N+2 pixels. I think that it's the 'removed' axes borders. How do I make it exactly N x N pixels (without cropping or re-sizing with an image editor)?
3 Comments
Accepted Answer
Oliver Woodford
on 8 Aug 2012
export_fig (or rather, print) appears to only export that part of the figure which is on screen. Make the figure small and use the -mX option. E.g. make the axes half the size and use -m2. The output then still won't be exactly right, but a simple solution is to crop the image programmatically.
0 Comments
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!