I want to use ArrayPlot instead of a DensityPlot because it seems to be faster (more here) I still want axes ticks to have real values and not array index.
I came up with following code which basically centers and scales ArrayPlot according to PlotRange given to Graphics:
arrayPlot[data_, opts___] :=
Module[{dim = Dimensions[data] // N, rls, range, imgSz, aRatio,
cf},
rls = {opts}~
Join~{PlotRange -> Transpose@{{1, 1}, dim},
ImageSize -> {500, 300}, ColorFunction -> "Rainbow"};
{range, cf} = {PlotRange, ColorFunction} /. rls;
aRatio = 1/Divide @@ (ImageSize /. rls);
Graphics[{Scale[#, (range.{-1, 1})*dim^-1] &@
Translate[#, (range.{1, 1} - dim)*0.5] &@
First@ArrayPlot[data, ColorFunction -> cf,
DataReversed -> True]}, Frame -> True,
AspectRatio -> aRatio, #] & @@ rls
];
It works OK and with:
dataP[m_, n_] := Table[i*Boole[j < i], {i, 1, m}, {j, 1, n}];
arrayPlot[dataP[100, 100], ImageSize -> {500, 500},
ColorFunction -> "Rainbow", PlotRange -> {{-21, 23}, {-1, 1}}]
I get

If I modify a arrayPlot with GridLines -> Automatic , GridLinesStyle -> Directive[Orange, Thickness[0.05]] added to Graphics, I get gridlines behind the plot. 
How can I make the grid lines appear in front of the plot? It is possible to add Mesh to ArrayPlot, but I want to avoid it since it will require synchronization of mesh with axes ticks.
Considering answer by 'rm -rf' the updated function is:
arrayPlot[data_, opts___] :=
Module[{dim = Dimensions[data] // N, rls, range, imgSz, aRatio,
cf},
rls = {opts}~
Join~{PlotRange -> Transpose@{{1, 1}, dim},
ImageSize -> {500, 300}, ColorFunction -> "Rainbow",
Method -> {"GridLinesInFront" -> True}};
{range, cf} = {PlotRange, ColorFunction} /. rls;
aRatio = 1/Divide @@ (ImageSize /. rls);
rls = Select[rls,
MatchQ[#, Alternatives @@ (#[[1]] & /@ Options[Graphics]) -> _] &];
Graphics[{Scale[#, (range.{-1, 1})*dim^-1] &@
Translate[#, (range.{1, 1} - dim)*0.5] &@
First@ArrayPlot[data, ColorFunction -> cf,
DataReversed -> True]}, Frame -> True,
AspectRatio -> aRatio, ##] & @@ rls
];
arrayPlot[dataP[100, 100], GridLines -> Automatic ,
ColorFunction -> "BlueGreenYellow",
GridLinesStyle -> Directive[Orange, Thickness[0.01]],
ImageSize -> {500, 500}, PlotRange -> {{-28, 23}, {-1, 1}}]
Answer
Edit:
As of V11 "GridLinesInFront" was documented together with "AxesInFront", "FrameInFront" and "TransparentPolygonMesh"
You need to use the undocumented option
Method -> {"GridLinesInFront" -> True}
to make the grid lines appear on top of your plot. This should give you:

Comments
Post a Comment