7
$\begingroup$

I have a matrix where each element represents a pixel. I want to plot the matrix (possibly with ArrayPlot). To suppress aliasing in the final graphic (which I will then export as a png), I am currently disabling any axis, caption and so on, and I'm forcing the size of the image to match the dimension of the matrix with ImageSize. I get the desired result, but I need to show the axes. Is there a way to do it?

$\endgroup$
3
  • $\begingroup$ Maybe you should check out the PixelConstrained option. $\endgroup$
    – einbandi
    Nov 20, 2012 at 14:30
  • $\begingroup$ @einbandi excellent, post it as an answer $\endgroup$ Nov 20, 2012 at 14:38
  • $\begingroup$ For a method including labels see my answer to (28246) $\endgroup$
    – Mr.Wizard
    Feb 4, 2015 at 14:44

2 Answers 2

7
$\begingroup$

Say you have a large array like:

array = Table[RandomReal[], {i, 200}, {j, 200}];

To align the cells of arraywith pixels, use the PixelConstrainedoption:

ArrayPlot[array, PixelConstrained -> True]

enter image description here

$\endgroup$
2
  • $\begingroup$ Unfortunately, this does not seem to help if I add the the Frame: the final output size (including frame and ticks) is the dimension of the matrix, and this of course causes stretching in the data plotting. I have tried also with Show[Image[mydata, ImageSize->Automatic, Magnification->1], Axes->True], same behaviour. $\endgroup$ Nov 21, 2012 at 10:33
  • $\begingroup$ This is true, I didn't notice that with my minimal example. You might be able work around that by using PixelConstrained->{2,2} (or even more?) and appropriate ImagePadding settings, along with a BaseStyle that fits your demands. This way you might get rid of the aliasing. $\endgroup$
    – einbandi
    Nov 21, 2012 at 22:21
4
$\begingroup$

Instead of using the PixelConstrained option, you can use the undocumented ImageSize syntax ImageSize -> 1 -> 2. For example:

array = Table[RandomReal[],{i,10},{j,10}];

plot = ArrayPlot[
    array,
    Frame->True,
    FrameLabel->{{x, y}, {a, b}},
    ImageSize -> 1 -> {50, 30},
    PlotRangePadding->0
]

enter image description here

Let's check the actual plot range size using my function GraphicsInformation:

<<GraphicsInformation`

"PlotRangeSize" /. GraphicsInformation[plot]

{500., 300.}

So, specifying that each element of array should have pixel dimensions of {50, 30} does indeed produce an image with plot range size {500, 300}.

$\endgroup$
1
  • $\begingroup$ (+1) Probably this technique also can be used as an answer for this question. $\endgroup$ Oct 7, 2018 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.