4
$\begingroup$

I have an ArrayPlot:

plot = ArrayPlot[RandomReal[1, {10, 20}], ColorFunction -> "Rainbow", 
  Frame -> None]

enter image description here

Now I want to plot axes around the plot labeled with numbers from my own plot range:

Graphics[Inset[plot, Scaled[{.5, .5}], Automatic, Scaled[1]], 
 Frame -> True, PlotRange -> {{11.5, 14.5}, {0.4, 0.8}}, 
 AspectRatio -> ImageAspectRatio@plot]

I used here the solution from @Kuba in this question.

enter image description here

How can I stretch the plot so that it fits into the frame without white borders, preserving the aspect ratio?

$\endgroup$
5
$\begingroup$

For minimal modification to your code zero PlotRangePadding and ImagePadding in the Inset:

Graphics[
 Inset[
   Show[plot, PlotRangePadding -> 0, ImagePadding -> 0],
     Scaled[{.5, .5}], Automatic, Scaled[1]
 ]
 , Frame -> True
 , PlotRange -> {{11.5, 14.5}, {0.4, 0.8}}
 , AspectRatio -> ImageAspectRatio @ plot
]

enter image description here

Alternatively construct the needed DataRange in ArrayPlot:

rangeFn[{{x_, X_}, {y_, Y_}}, {h_, w_}] :=
   {{x + (X - x)/(2 w), X - (X - x)/(2 w)},
    {y + (Y - y)/(2 h), Y - (Y - y)/(2 h)}}

data = RandomReal[1, {10, 20}];

range = rangeFn[{{11.5, 14.5}, {0.4, 0.8}}, Dimensions@data];

p2 = ArrayPlot[data
      , ColorFunction -> "Rainbow"
      , DataRange -> range
      , AspectRatio -> 1/2
      , PlotRangePadding -> None
     ];

Show[p2, FrameTicks -> Automatic]

enter image description here

Notes:

  • Show is necessary here as adding FrameTicks to the ArrayPlot expression does not yield a reasonable tick spacing.
  • rangeFn is needed to counteract "For ArrayPlot, the settings for DataRange refer to the coordinates of the centers of each cell."
$\endgroup$
  • $\begingroup$ Thanks a lot … but I still see in my Graphics output a very small white gap above the lower horizontal axis and also left from the right vertical axis. I am using Mathematica 11.3.0.0 $\endgroup$ – mrz Jul 24 '18 at 11:46
  • 1
    $\begingroup$ @Lenoil please try using Inset[Show[plot, PlotRangePadding -> 0, ImagePadding -> 0, ImageMargins -> 0], Scaled[{.5, .5}], Automatic, Scaled[1]] and report the result. $\endgroup$ – Mr.Wizard Jul 24 '18 at 11:51
  • $\begingroup$ Mr.Wizard this is the solution … thank you. $\endgroup$ – mrz Jul 24 '18 at 11:57
  • $\begingroup$ @Lenoil Glad I could help! :-) I think ImageMargins is not needed so I left that out of my revised answer; please let me know if this is incorrect. I also added an alternative approach you may consider. $\endgroup$ – Mr.Wizard Jul 24 '18 at 12:03
  • $\begingroup$ I tried both of your solutions and they work as you wrote. Thank you for the notes. $\endgroup$ – mrz Jul 24 '18 at 13:30
0
$\begingroup$
Graphics[
 Inset[plot, Scaled[{.5, .5}], Automatic, Scaled[1.072]],
 Frame -> True,
 PlotRange -> {{11.5, 14.5}, {0.4, 0.8}},
 AspectRatio -> ImageAspectRatio@plot]
$\endgroup$
  • $\begingroup$ Thank you but the factor 1.072 from where did you get it? I need some general solution. $\endgroup$ – mrz Jul 24 '18 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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