4
$\begingroup$

I would like to overlay ListPlot on ArrayPlot. I have explored a number of posts, but I did not manage to achieve the desired outcome. Hence, the reason for the post. Following is a visual representation of what I want to achieve.

matrix

  1. Set custom axes;
  2. Align ArrayPlot to ListPlot axes;
  3. Plot points with labels;

Here is the grid I would like to overlay...

colors = RGBColor /@ {"#c7e8ac","#ffeca9","#ff8f80"}

grid = {
{Yellow,Yellow, Red, Red, Red},
{Green, Yellow, Yellow, Red, Red},
{Green, Yellow, Yellow, Yellow, Red},
{Green, Green, Yellow, Yellow, Yellow},
{Green, Green, Green, Green, Yellow}
} /.{
Green-> colors[[1]]
, Yellow-> colors[[2]]
, Red-> colors[[3]]
};

ArrayPlot[
 grid
 , Mesh -> True
 , Axes -> True
]

and the data would be generated in the following way...

Outer[{#2, #1, #1*#2} &, Range[0.01, 1, .01], Subdivide[1000, 20000000, 1000]] // Flatten[#, 1] &;

I have tried to implement a solution using Epilog and Inset. However, the results are suboptimal...

matrix

The code used for the above...

ListPlot[
 {{5000, 0.05}}
 , PlotRange -> {{0, 10^6}, {0, 1}}
 Epilog -> Inset[
   ArrayPlot[
    grid
    , Mesh -> True
    ]
   , Scaled[{0.02, 0.02}]
   , Scaled[{0, 0}]
   , Scaled[1]
   ]
 ]
$\endgroup$
3
  • 1
    $\begingroup$ does ListPlot[{{{5000, 0.05}}, {{500000, 0.7}}}, PlotLabels -> {"Label 1", "Label 2"}, PlotStyle -> Directive[Black, PointSize[Large]], PlotRange -> {{0, 10^6}, {0, 1}}, Prolog -> Inset[ArrayPlot[grid, Mesh -> True, PlotRangePadding -> None], Scaled[{0, 0}], Scaled[{0, 0}], Scaled[1]], AspectRatio -> 1] give anything close to what you need? $\endgroup$
    – kglr
    Commented Sep 7, 2021 at 11:08
  • $\begingroup$ @kglr hello, and yes! $\endgroup$ Commented Sep 7, 2021 at 11:18
  • $\begingroup$ w.doroskevic, posted the comment as an answer. $\endgroup$
    – kglr
    Commented Sep 7, 2021 at 11:29

1 Answer 1

3
$\begingroup$

(1) Use the option AspectRatio -> 1 in ListPlot[...], (2) use Prolog (instead of Epilog) so that the points and lines are not covered by inset object, (3) add the options PlotRangePadding -> None and ImagePadding -> None to ArrayPlot:

lp = ListPlot[{{{50000, 0.05}}, {{500000, 0.7}}}, 
  PlotLabels -> {"Label 1", "Label 2"}, 
  PlotStyle -> Directive[Black, PointSize[Large]], 
  PlotRange -> {{0, 10^6}, {0, 1}}, 
  Prolog -> 
   Inset[ArrayPlot[grid, Mesh -> True,
           PlotRangePadding -> None, ImagePadding -> None], 
    Scaled[{0, 0}], Scaled[{0, 0}], Scaled[1]], AspectRatio -> 1]

enter image description here

Post-process to change the styling of callout lines:

lp /. {Directive[AbsoluteThickness[4], ___], ___} :> {} /. 
 b_BSplineCurve :> {Black, b}

enter image description here

$\endgroup$
5
  • $\begingroup$ For completeness, could you please adjust the Ticks for both x and y axes as in the picture posted as the desired outcome? I am anticipating alignment issues there, and I would appreciate if you could advise on that. $\endgroup$ Commented Sep 7, 2021 at 11:36
  • $\begingroup$ @e.doroskevic, looks like the ticks in your posted picture are not linearly scaled. $\endgroup$
    – kglr
    Commented Sep 7, 2021 at 12:03
  • $\begingroup$ I can appreciate this complecates matters. Any advice how this could be incorporated into the model above? $\endgroup$ Commented Sep 7, 2021 at 12:32
  • $\begingroup$ You might consider using the option ScalingFunctions with ListPlot, or, using ListLogLinearPlot $\endgroup$
    – kglr
    Commented Sep 7, 2021 at 15:19
  • $\begingroup$ Thank you for the answer provided. After reading the documentation for the Scaling Functions, I remain unsure how to fix the axes. I will spend some time on it, and hopefully come up with a solution. $\endgroup$ Commented Sep 7, 2021 at 18:43

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.