4

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?

mrz
  • 11,686
  • 2
  • 25
  • 81

2 Answers2

5

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."
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 – mrz Jul 24 '18 at 11:46
  • 1
    @Lenoil please try using Inset[Show[plot, PlotRangePadding -> 0, ImagePadding -> 0, ImageMargins -> 0], Scaled[{.5, .5}], Automatic, Scaled[1]] and report the result. – Mr.Wizard Jul 24 '18 at 11:51
  • Mr.Wizard this is the solution … thank you. – mrz Jul 24 '18 at 11:57
  • @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. – Mr.Wizard Jul 24 '18 at 12:03
  • I tried both of your solutions and they work as you wrote. Thank you for the notes. – mrz Jul 24 '18 at 13:30
  • Could you please have a look on that question: https://mathematica.stackexchange.com/questions/189782/two-data-sets-superposed-using-two-different-axes-styles. I am looking for a solution with pure Mathematica 11.3 commands. – mrz Jan 18 '19 at 22:10
  • @mrz I am still using version 10.1. Are you saying that behavior has changed in 11.3 and the answers to (627) no longer work as shown? – Mr.Wizard Jan 19 '19 at 00:08
0
Graphics[
 Inset[plot, Scaled[{.5, .5}], Automatic, Scaled[1.072]],
 Frame -> True,
 PlotRange -> {{11.5, 14.5}, {0.4, 0.8}},
 AspectRatio -> ImageAspectRatio@plot]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • Thank you but the factor 1.072 from where did you get it? I need some general solution. – mrz Jul 24 '18 at 10:59