8
$\begingroup$

I'm drawing several ArrayPlots and I would like they all have same size of cells. Can I instead of setting ArrayPlot size and AspectRatio, set fixed cell size of ArrayPlot?

ArrayPlot[Table[0 , {x, 4}, {y, 5}], Mesh -> True]
ArrayPlot[Table[0, {x, 4}, {y, 10}], Mesh -> True]
ArrayPlot[Table[0 , {x, 10}, {y, 10}], Mesh -> True]

Now I have this

enter image description here

but I need this

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Set the same ImageSize and AspectRatio for all of them, e.g. ArrayPlot[yourData, ImageSize -> Medium, AspectRatio -> 1]. $\endgroup$
    – MarcoB
    Commented Jan 31, 2017 at 20:15
  • $\begingroup$ @MarcoB It will give me the ArrayPlots of the same size. I do not want it! I want the same size of small cells inside the ArrayPlots $\endgroup$ Commented Jan 31, 2017 at 21:21
  • $\begingroup$ Related: (14940), (28246) $\endgroup$
    – Mr.Wizard
    Commented Jan 31, 2017 at 21:46

3 Answers 3

11
$\begingroup$

As an alternative to PixelConstrained, you can use the somewhat new (in M8) syntax for ImageSize:

ArrayPlot[Table[0, {x,4}, {y,5}], Mesh->True, ImageSize->1->25]
ArrayPlot[Table[0, {x,4}, {y,10}], Mesh->True, ImageSize->1->25]
ArrayPlot[Table[0, {x,10}, {y,10}], Mesh->True, ImageSize->1->25]

enter image description here

$\endgroup$
4
  • 3
    $\begingroup$ Neat! Is this documented? $\endgroup$
    – Mr.Wizard
    Commented Feb 1, 2017 at 1:12
  • $\begingroup$ @Mr.Wizard I couldn't find it in the docs, but it turns out that it had been mentioned in this site a couple of years ago. Even so, I'm still not sure that I fully understand it's usage, so I'd love for Carl to weigh in. $\endgroup$
    – MarcoB
    Commented Feb 1, 2017 at 13:53
  • $\begingroup$ @MarcoB I'd forgotten about that, as usual. I also hope Carl has more to say. $\endgroup$
    – Mr.Wizard
    Commented Feb 1, 2017 at 15:02
  • 1
    $\begingroup$ I don't know why it isn't documented yet. The basic usage is something like ImageSize->userUnits->pixels, where userUnits is the usual ImageSize spec. One convenient syntax is something like ImageSize->Automatic->500, which says that the PlotRange should be 500 pixels wide, and so you can have a column of such graphics with identical widths ignoring ticks, labels, etc. $\endgroup$
    – Carl Woll
    Commented Feb 1, 2017 at 16:52
6
$\begingroup$

I would use PixelConstrained but you will need a fix from ArrayPlot: PixelConstrained doesn't account for Mesh thickness to avoid a small bug.

fixPC = Show[#, PlotRangePadding -> 1, ImageSize -> 2 + PlotRange[#][[All, 2]]] &;

Now:

ArrayPlot[Table[0, {x, 4}, {y, 5}], Mesh -> True, PixelConstrained -> 25] // fixPC
ArrayPlot[Table[0, {x, 4}, {y, 10}], Mesh -> True, PixelConstrained -> 25] // fixPC
ArrayPlot[Table[0, {x, 10}, {y, 10}], Mesh -> True, PixelConstrained -> 25] // fixPC

enter image description here

$\endgroup$
0
5
$\begingroup$

I think you have to calculate the ImageSize based on the size of the table.

ClearAll[ap];
ap[table_?MatrixQ, im_Integer: 10, opts : OptionsPattern[ArrayPlot]] :=
  ArrayPlot[table, opts, ImageSize -> im*Length[First@table]+2]

ap[Table[0, {x, 4}, {y, 5}], Mesh -> True]
ap[Table[0, {x, 4}, {y, 10}], Mesh -> True]
ap[Table[0, {x, 10}, {y, 10}], Mesh -> True]

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ This produces subtle distortions in the mesh that are avoided using PixelConstrained. $\endgroup$
    – Mr.Wizard
    Commented Jan 31, 2017 at 21:45
  • $\begingroup$ @Mr.Wizard Not in pdf/svg, perhaps? I'm not sure my eyes are good enough for screen pixels. I see small distortions with all ways. The only way I could find to resize PixelConstrained plots is with Magnify. $\endgroup$
    – Michael E2
    Commented Jan 31, 2017 at 23:20
  • $\begingroup$ In vector formats rasterization is deferred but they are still subject to the same issue when converted to raster, i.e. displayed on screen, so if you want pixel-perfect raster you would at that time need to specify an exact pixel dimension. Not being able to freeform resize is rather the point; there is only one dimension that is "correct" and exact multiples of it. Use a different option value for PixelConstrained if you need a different image size. $\endgroup$
    – Mr.Wizard
    Commented Feb 1, 2017 at 11:21
  • $\begingroup$ @Mr.Wizard Then I think my fix fixes that. Provide you display at 100% & don't resize. What do you think? (Note my notebooks are set 150%, which explains why everything looks slightly wonky. PixelConstrained does not really constrain to pixels, it turns out.) $\endgroup$
    – Michael E2
    Commented Feb 1, 2017 at 11:31
  • $\begingroup$ I would not expect magnification to work any other way. You need PlotRangePadding -> 0 on my system to prevent the distortion I mentioned, but that fails if you add Axes -> True. Compare e.g. ArrayPlot[BoxMatrix[0, 50], Mesh -> True, PixelConstrained -> 3, Axes -> True] // fixPC to ap[BoxMatrix[0, 50], 3, Mesh -> True, PlotRangePadding -> 0, Axes -> True], at 100% magnification of course. $\endgroup$
    – Mr.Wizard
    Commented Feb 1, 2017 at 11:45

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.