1
$\begingroup$

My question has two parts:

First Part:

I have the following array:

data = {{0.00545, 0.031286, 0.047969, 0.03371, 0.067131, 0.069519, 0.055705, 
  0.075217}, {0.003295, 0.029857, 0.082773, 0.09734, 0.105596, 
  0.09507, 0.074274, 0.066184}, {0.004738, 0.051967, 0.119394, 
  0.121927, 0.133162, 0.136331, 0.073764, 0.064085}, {0.007415, 
  0.100494, 0.134302, 0.152252, 0.237188, 0.115811, 0.071255, 
  0.053821}, {0.016775, 0.089863, 0.144896, 0.185504, 0.148454, 
  0.103395, 0.064046, 0.026352}, {0.011129, 0.096169, 0.292988, 
  0.138956, 0.125583, 0.097096, 0.052228, 0.035961}, {0.011951, 
  0.486986, 0.09108, 0.081619, 0.088997, 0.040025, 0.017843, 
  0.022213}, {0.042095, 0.033775, 0.029787, 0.043058, 0.018558, 
  0.010313, 0.005565, 0.010936}}

I define the following to change the tickers:

g1 = {"[0,5)", "[5,18)", "[18,30)", "[30,40)", "[40,50)", "[50,60)", 
   "[60,70)", " 70+"};
g2 = Range[8];
g3 = Transpose[{g2, g1}];
g4 = Transpose[{g2 // Reverse, g1}];

I then Plot as:

ArrayPlot[data, ColorFunction -> "Rainbow", PlotTheme -> "Detailed", 
 Mesh -> True, Ticks -> {{g4, None}, {g3, None}}]

However this does not show any tickers, I wonder what am I doing wrong?

Second Part: If you plot using the command I have you see that the plot legends are 0.1, 0.2, 0.3 and 0.4, I wonder how can I change them to my own list {0,2,4,6,8} (as you can see there are two more entries on the list). I do:

ArrayPlot[data, ColorFunction -> "Rainbow", PlotTheme -> "Detailed", 
 Mesh -> True, Ticks -> {{g4, None}, {g3, None}}, 
 PlotLegends -> {0, 2, 4, 6, 8}]

and this gives a plot legend with two tickers of 0 and 2. I wonder what am I doing wrong?

$\endgroup$
3
  • 2
    $\begingroup$ For the first part, use FrameTicks -> {{g4, None}, {g3, None}} and add the option Frame ->True? $\endgroup$
    – kglr
    May 30, 2020 at 4:25
  • $\begingroup$ @kglr beat me to it! Here’s why you need the Frame -> True. (Scroll down slightly) $\endgroup$ May 30, 2020 at 4:32
  • $\begingroup$ I don’t understand your second part. Your data does not contain values that high. What should the 2, 4, 8... indicate? $\endgroup$
    – MarcoB
    May 30, 2020 at 4:48

1 Answer 1

2
$\begingroup$
  1. Use FrameTicks instead of Ticks and add the option Frame->True.

  2. Rescale the in put data to run from 0 to 8.

ArrayPlot[Rescale[data, MinMax@data, {0, 8}], 
 ColorFunction -> "Rainbow", PlotTheme -> "Detailed", Mesh -> True, 
 Frame -> True, FrameTicks -> {{g4, None}, {g3, None}} ]

enter image description here

Add the option PlotLegends -> Range[0, 8] to get

enter image description here

$\endgroup$
2
  • $\begingroup$ How do you ensure the labels on each tickmark are appropriately spaced? I had to use a specified ImageSize on mobile. $\endgroup$ May 30, 2020 at 4:34
  • 2
    $\begingroup$ @CATrevillian, In version 11.3 (Windows 10) the tick labels are automatically centered at the ticks. $\endgroup$
    – kglr
    May 30, 2020 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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